カスタム投稿タイプのカテゴリを表示するにはどうすればよいですか?
2 回答
- 投票
-
- 2013-08-01
投稿タイプと分類法を登録するportfolio-type.phpからコードを削除します(9行目以降).
次のコード(portfolio-type.php内)を使用して、投稿タイプ「portfolio」を登録します
function portfolio_register() { $labels = array( 'name' => _x('Portfolio', 'post type general name'), 'singular_name' => _x('Portfolio Item', 'post type singular name'), 'add_new' => _x('Add New', 'portfolio item'), 'add_new_item' => __('Add New Portfolio Item'), 'edit_item' => __('Edit Portfolio Item'), 'new_item' => __('New Portfolio Item'), 'view_item' => __('View Portfolio Item'), 'search_items' => __('Search Portfolio Items'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => 8, 'supports' => array('title','editor','thumbnail') ); register_post_type( 'portfolio' , $args ); } add_action('init', 'portfolio_register');
次のコード(portfolio-type.php内)を使用して、投稿タイプ「portfolio」の分類法「portfolio_categories」を登録し、階層化(カテゴリなど)します
function create_portfolio_taxonomies() { $labels = array( 'name' => _x( 'Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Category', 'taxonomy singular name' ), 'search_items' => __( 'Search Categories' ), 'all_items' => __( 'All Categories' ), 'parent_item' => __( 'Parent Category' ), 'parent_item_colon' => __( 'Parent Category:' ), 'edit_item' => __( 'Edit Category' ), 'update_item' => __( 'Update Category' ), 'add_new_item' => __( 'Add New Category' ), 'new_item_name' => __( 'New Category Name' ), 'menu_name' => __( 'Categories' ), ); $args = array( 'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags) 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'categories' ), ); register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args ); } add_action( 'init', 'create_portfolio_taxonomies', 0 );
次に、次のコードを使用して、テンプレートファイル(index.phpなど)の分類用語を取得します
<?php $taxonomy = 'portfolio_categories'; $terms = get_terms($taxonomy); // Get all terms of a taxonomy if ( $terms && !is_wp_error( $terms ) ) : ?> <ul> <?php foreach ( $terms as $term ) { ?> <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li> <?php } ?> </ul> <?php endif;?> ?>
説明が必要な場合はお知らせください.
Remove your code from portfolio-type.php that registers post type and taxonomy (line 9 onwards).
Use the following code (in portfolio-type.php) to register the post type "portfolio"
function portfolio_register() { $labels = array( 'name' => _x('Portfolio', 'post type general name'), 'singular_name' => _x('Portfolio Item', 'post type singular name'), 'add_new' => _x('Add New', 'portfolio item'), 'add_new_item' => __('Add New Portfolio Item'), 'edit_item' => __('Edit Portfolio Item'), 'new_item' => __('New Portfolio Item'), 'view_item' => __('View Portfolio Item'), 'search_items' => __('Search Portfolio Items'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => 8, 'supports' => array('title','editor','thumbnail') ); register_post_type( 'portfolio' , $args ); } add_action('init', 'portfolio_register');
Use the following code (in portfolio-type.php) to register a taxonomy "portfolio_categories" for the post type "portfolio", make it hierarchical (like categories)
function create_portfolio_taxonomies() { $labels = array( 'name' => _x( 'Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Category', 'taxonomy singular name' ), 'search_items' => __( 'Search Categories' ), 'all_items' => __( 'All Categories' ), 'parent_item' => __( 'Parent Category' ), 'parent_item_colon' => __( 'Parent Category:' ), 'edit_item' => __( 'Edit Category' ), 'update_item' => __( 'Update Category' ), 'add_new_item' => __( 'Add New Category' ), 'new_item_name' => __( 'New Category Name' ), 'menu_name' => __( 'Categories' ), ); $args = array( 'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags) 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'categories' ), ); register_taxonomy( 'portfolio_categories', array( 'portfolio' ), $args ); } add_action( 'init', 'create_portfolio_taxonomies', 0 );
Then use the following code to retrieve taxonomy terms in template files (like index.php)
<?php $taxonomy = 'portfolio_categories'; $terms = get_terms($taxonomy); // Get all terms of a taxonomy if ( $terms && !is_wp_error( $terms ) ) : ?> <ul> <?php foreach ( $terms as $term ) { ?> <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li> <?php } ?> </ul> <?php endif;?> ?>
Let me know if you need any clarification.
-
Portfolio-type.phpのコードを指定したコードに置き換えたところ、カテゴリが表示されましたが、何らかの理由でプロジェクトがフィルタリングされていません.I replaced the code in my portfolio-type.php with the code you provided and I got the categories to show up but they're not filtering the projects for some reason.
- 0
- 2013-08-01
- Laniakea
-
私が提供した最初の2つのコードスニペットは、バックエンド機能用でした.私が提供した3番目のスニペットでは、カテゴリ名を取得できます. もう一度あなたのウェブサイトを見て、あなたが今欠けているものを見てみましょう.The first two code snippets I provided was for the back-end functionality. The third snippet I provided allows you to retrieve the category names. Let me go through your website again and see what you are missing now.
- 0
- 2013-08-01
- Rahul Verma
-
あなたが今持っているphpコード(ポートフォリオセクション全体)を投稿することは可能でしょうか?Would it be possible for you to post the php code (for the whole portfolio section) you have now?
- 0
- 2013-08-01
- Rahul Verma
-
コードをコミットしました[ここ](https://bitbucket.org/mmetsalu/must-testing/commits/67155426612a79499d193f4e01791183051a90a5).I've committed the code [here](https://bitbucket.org/mmetsalu/must-testing/commits/67155426612a79499d193f4e01791183051a90a5).
- 0
- 2013-08-01
- Laniakea
-
カテゴリフィルタリングに関する新しいスレッドを開始します.I will start a new thread regarding the categories filtering.
- 0
- 2013-08-05
- Laniakea
-
これは、カスタム分類法 `function displayCategories($post_type_name){を設定した後にカテゴリをロードするためのより良い方法かもしれません. $ customPostTaxonomies=get_object_taxonomies($post_type_name); if(count($ customPostTaxonomies)> 0){ foreach($ customPostTaxonomies as $tax){ $ args=array( 'orderby'=> '名前'、 'show_count'=> 1、 'pad_counts'=> 0、 '階層'=> 1、 '分類法'=> $tax、 'title_li'=> '' ); wp_list_categories($ args); } } } `This might be a better way to load the categories after setting up the custom taxonomy `function displayCategories($post_type_name) { $customPostTaxonomies = get_object_taxonomies($post_type_name); if(count($customPostTaxonomies) > 0) { foreach($customPostTaxonomies as $tax) { $args = array( 'orderby' => 'name', 'show_count' => 1, 'pad_counts' => 0, 'hierarchical' => 1, 'taxonomy' => $tax, 'title_li' => '' ); wp_list_categories( $args ); } } }`
- 0
- 2016-09-28
- Eyo Okon Eyo
-
カテゴリに投稿がない場合でもリストを表示するには、 `$ args=array( 'hide_empty'=>false、);` `$terms=get_terms($taxonomy、$ args)`である必要があります.Should be `$args = array( 'hide_empty' => false, );` `$terms = get_terms($taxonomy, $args)` to see list even when categories do not have posts.
- 0
- 2018-02-06
- X9DESIGN
-
カスタム投稿タイプがあります.私がやりたいのは、プロジェクトのすぐ上にプロジェクトカテゴリを表示して、訪問者がそれに応じてプロジェクトをフィルタリングできるようにすることです.
私の
functions.php
には次のものがあります:portfolio-type.php
内:そして最後に私のindex.phpに私は持っています: