2017-08-17 15 views
0

私はワードプレス開発の新人です。私はワードプレスの特定のページに特定のカテゴリを表示する方法を知りたい。私は私のプロジェクトでそれを行う必要があります。Wordpressの特定のページに特定のカテゴリのみを表示するには?

+0

ようこそ。一般的なヘルプや推奨事項について質問することはお勧めできません。[許可されるトピック](https://stackoverflow.com/help/on-topic)を参照してください。これはコード作成サービスではなく、あなたが問題を調査し、投稿する前に解決しようとしていることが予想されます。 [Stack Overflowユーザーにどの程度の研究努力が期待されていますか?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users)と[どのように私は良い質問をする](https://stackoverflow.com/help/how-to-ask) – FluffyKitten

答えて

0

は、リンクとしてカテゴリのリストを表示します。

基本的には、この機能をwp_list_categories();と呼んで、カテゴリを表示する場所に表示する必要があります。

このオプションは、表示するカテゴリIDのリストを受け入れるincludeを使用します。この

$args = array(
    'hide_empty' => 0, //Show me all the categories, even the empty ones 
    'orderby' => 'count', //which accepts a string. You can pick from the following options: ID to order the categories by their ID (no, really?), name to sort them alphabetically (the default value), slug to sort them in the alphabetical order of their slugs, and count to order by the number of posts they contain. 
    'order' => 'DESC', //The chosen order can be reversed by setting DESC (descending) as a value for the order option (by default this option is set to ASC (ascending)). 
    'include' => '15,16,9' 
); 

wp_list_categories($args); 

同様

$categories = get_categories(array(
    'orderby' => 'name', 
    'order' => 'ASC', 
    'include' => '15,16,9' 
)); 

foreach($categories as $category) { 
    $category_link = sprintf( 
     '<a href="%1$s" alt="%2$s">%3$s</a>', 
     esc_url(get_category_link($category->term_id)), 
     esc_attr(sprintf(__('View all posts in %s', 'textdomain'), $category->name)), 
     esc_html($category->name) 
    ); 

    echo '<p>' . sprintf(esc_html__('Category: %s', 'textdomain'), $category_link) . '</p> '; 
    echo '<p>' . sprintf(esc_html__('Description: %s', 'textdomain'), $category->description) . '</p>'; 
    echo '<p>' . sprintf(esc_html__('Post Count: %s', 'textdomain'), $category->count) . '</p>'; 
} 

wp_list_categoriesget_categories 間プレスの機能get_categories()wp_list_categories()get_categories();機能を使用してカテゴリを表示する別の方法は、ほぼTであります彼は同じです。彼らはほとんど同じパラメータを使用していますが、それらの間に違いがあります:

get_categories()関数は、クエリパラメータに一致するカテゴリオブジェクトの配列を返します。 wp_list_categories()には、カテゴリのリストがリンクとして表示されます。 WordPressの開発者のdocumention:

get_categories():http://codex.wordpress.org/Function_Reference/get_categories

wp_list_categories():スタックオーバーフローにhttp://codex.wordpress.org/Function_Reference/wp_list_categories

関連する問題