$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display Custom Taxonomy names|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display Custom Taxonomy names

matteradmin7PV0评论

Im trying to display a custom taxonomy for a custom post type. So this taxonomy is specific to this custom post type. Unfortunately I cant get them to display. Here is my code in functions.php to register the custom taxonomy:

add_action('init', 'products_categories', 0);

function products_categories(){
$labels = array ('name' => _x('Product Categories','taxonomy general name'),
                'singular_name' =>_x('Product Category','taxonomy singular name'),
                'serch_items' => __('Search Product Categories'),
                'popular_items' => ('Popular Product Categories'),
                'all_items' => __('All Product Categories'),
                'parent_item' => null,
                'parent_item_colon' => null,
                'edit_item' => __('Edit Product Category'),
                'update_item' => __('Update Product Category'),
                'add_new_item' => __('Add Product Category'),
                'new_item_name' => __('New Product Category'),
                'separate_items_with_commas' => __('Seperate Product Categories with commas'),
                'add_or_remove_items' => __('Add or remove Product Categories'),
                'choose_from_most_used' => __('Most Used Product Categories'),
                'menu_name' => __('Product Categories'),
                );

register_taxonomy('product_categories', 'products', array(
'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'product_category' ),
));
}

And here im trying to display them:

<?php } 
    $terms = get_terms('taxonomy'=>'product_category', 'hide_empty'=> false,);
        foreach ( $terms as $term) {
?>
<a href=""><?php echo $term->name; ?></a>
<?php 
 }?>

Do I need to have this running in a query of some sort first?

Im trying to display a custom taxonomy for a custom post type. So this taxonomy is specific to this custom post type. Unfortunately I cant get them to display. Here is my code in functions.php to register the custom taxonomy:

add_action('init', 'products_categories', 0);

function products_categories(){
$labels = array ('name' => _x('Product Categories','taxonomy general name'),
                'singular_name' =>_x('Product Category','taxonomy singular name'),
                'serch_items' => __('Search Product Categories'),
                'popular_items' => ('Popular Product Categories'),
                'all_items' => __('All Product Categories'),
                'parent_item' => null,
                'parent_item_colon' => null,
                'edit_item' => __('Edit Product Category'),
                'update_item' => __('Update Product Category'),
                'add_new_item' => __('Add Product Category'),
                'new_item_name' => __('New Product Category'),
                'separate_items_with_commas' => __('Seperate Product Categories with commas'),
                'add_or_remove_items' => __('Add or remove Product Categories'),
                'choose_from_most_used' => __('Most Used Product Categories'),
                'menu_name' => __('Product Categories'),
                );

register_taxonomy('product_categories', 'products', array(
'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'product_category' ),
));
}

And here im trying to display them:

<?php } 
    $terms = get_terms('taxonomy'=>'product_category', 'hide_empty'=> false,);
        foreach ( $terms as $term) {
?>
<a href=""><?php echo $term->name; ?></a>
<?php 
 }?>

Do I need to have this running in a query of some sort first?

Share Improve this question edited Nov 21, 2018 at 10:31 Zayd Bhyat asked Nov 21, 2018 at 10:09 Zayd BhyatZayd Bhyat 892 silver badges15 bronze badges 1
  • there you have registered taxonomy 'product_categories' but you trying get_terms with a different name as 'product_category'. – Aishan Commented Nov 21, 2018 at 10:22
Add a comment  | 

2 Answers 2

Reset to default 1

it looks like your get_terms() call is incorrect, the argument is not an array and you have a typo in taxonomy name ('product_category' instead of 'product_categories') it should be

<?php 
$terms = get_terms( array( 
    'taxonomy'=>'product_categories', 
    'hide_empty'=> false 
) ); 
?>

If this will not help it would be best if you could let us know if you are seeing any error message and if you do paste the error here.

I managed to solve this and I will post the answer:

<?php
  $args = array('number' => '1',);
  $terms = get_terms('recipes', $args );
    foreach( $terms as $term ){
    echo '<div class="title">' . $term->name . 'recipe</div>';
    } 

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far