Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

php - Problem to display taxonomies terms in wordpress

i created a custom post type (call bookmaker_post_type) with more custom taxonomies asociated but i have problem to display the terms selected of each taxonomy. I tried to use the function the_terms <td><?php the_terms( $post->ID, 'countrie'); ?></td> in post single but not show nothing. I also tried to insert the function in the loop but nothing. Where i wrong?

taxonomy code

// Add new taxonomy Countries, make it hierarchical (like categories)
$labels = array(
    'name'              => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
    'singular_name'     => _x( 'Countrie', 'taxonomy singular name', 'textdomain' ),
    'search_items'      => __( 'Search Countries', 'textdomain' ),
    'all_items'         => __( 'All Countries', 'textdomain' ),
    'parent_item'       => __( 'Parent Countrie', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Countrie:', 'textdomain' ),
    'edit_item'         => __( 'Edit Countrie', 'textdomain' ),
    'update_item'       => __( 'Update Countrie', 'textdomain' ),
    'add_new_item'      => __( 'Add New Countrie', 'textdomain' ),
    'new_item_name'     => __( 'New Countrie Name', 'textdomain' ),
    'menu_name'         => __( 'Countrie', 'textdomain' ),
);
 
$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'countrie' ),
);
 
register_taxonomy( 'countrie', array( 'bookmaker' ), $args );
     
    unset( $args );
    unset( $labels );
}
question from:https://stackoverflow.com/questions/65860072/problem-to-display-taxonomies-terms-in-wordpress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$post isn't declared. So you can't use $post->ID. You have to either declare it using global $post; or simply use get_the_ID().

the_terms( get_the_ID(), 'countrie' );

On a side note, plural is countries singular is country not countrie ... but that's just English grammar.


In a loop

<?php
if ( have_posts() ):
  $i = 0;
  while( have_posts() ): the_post();
    $i++;
    if ( $i > 1 )
      echo '<hr>';
    the_terms( get_the_ID(), 'countrie' );
    the_title( '<h1>', '</h1>' );
  endwhile;
endif; ?>

Learn more


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...