my taxonomy-$taxonomy.php page post not show?

こ雲淡風輕ζ 提交于 2019-12-25 07:34:38

问题


I created a custom post taxonomy.Now i want to show all specific post by specific taxonomy. so I created a taxonomy-product_cat.php page.

here is product page get term and link code--

<div class="side_box side_box_1 red5">
    <h5><a href="#" class="tgl_btn">Filter Products</a></h5>
    <h6>Brand</h6>
    <?php $topics = get_terms('product_cat');
        echo '<ul class="advanced-filters tgl_c">';
        foreach ($topics as $topic) {
            echo '<li class="advanced-filter" data-group="Brand"><a href="'.get_term_link($topic).'">'.$topic->name.'</a></li>';
        }
        echo '</ul>';?>
</div> 

here is custom post taxonomy code---

function product_taxonomy() {
register_taxonomy(
    'product_cat',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
    'product',                  //post type name
    array(
        'hierarchical'          => true,
        'label'                 => 'product Category',  //Display name
        'query_var'             => true,
        'show_admin_column' => true,
        'rewrite'               => array(
            'slug'              => 'product-category', // This controls the base slug that will display before each term
            'with_front'        => false // Don't display the category base before
            )
        )
);
}
add_action( 'init', 'product_taxonomy');

And here is taxonomy-product_cat.php page code--

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $unique = "_product_"; ?>
  <div class="col-md-3 col-xs-6 element mb30">
    <div class="main_box">
      <div class="box_1">
        <div class="product-image">
          <a href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail( $post->ID, 'product-image',array('class' => 'img-responsive') );?>
          </a>
        </div>
        <div class="overlay hidden-sm hidden-xs">
          <a href="<?php the_permalink(); ?>" class="btn_c more_btn">More Info</a>   
        </div>
      </div>
      <div class="desc">
        <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
        <p><?php echo get_post_meta(get_the_ID(),$unique.'brand_name',true); ?></p>
      </div>
    </div>
  </div>
  <?php endwhile; ?>
  <?php else :?>
  <h3><?php _e( 'Not Found Any Product.' ); ?></h3>
<?php endif ?> 

But the result is Not Found Any Product.So please someone help me how can i fix this problem.Thanks


回答1:


see documentation

https://developer.wordpress.org/reference/functions/get_terms/

specifically

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

$terms = get_terms( array( 'taxonomy' => 'post_tag', 'hide_empty' => false, ) );

instead of the first parameter being the taxonomies.

  get_terms('product_cat');

You should do

 get_terms(array(
   'taxonomy' => 'product_cat'
));

Not sure if that is the issue, but seems the most obvious thing.

UPDATE

Did you try this

get_terms(array(
   'taxonomy' => 'product_cat',
   'hide_empty' => 0
));

This will show the taxonomy if you didn't insert any actual terms with

wp_insert_term()

As per this page, https://wordpress.org/support/topic/get_terms-does-not-return-my-custom-taxonomy-terms

I don't typically work with WP on this level, but my google skills are unmatched ... lol



来源:https://stackoverflow.com/questions/38560652/my-taxonomy-taxonomy-php-page-post-not-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!