Php 显示wordpress类别名称和相关帖子

Php 显示wordpress类别名称和相关帖子,php,wordpress,taxonomy,Php,Wordpress,Taxonomy,我想在顶部显示类别名称,然后在其下方显示属于特定类别的帖子 这应该发生在我的20个类别中,结果显示在一个页面上 这是我一直在尝试的东西,但它不起作用 <?php $catquery = new WP_Query( 'cat=finance-training-seminars&posts_per_page=-1&post-type=dt_portfolio' ); ?> <ul> <?php while($catquery->have_posts

我想在顶部显示类别名称,然后在其下方显示属于特定类别的帖子

这应该发生在我的20个类别中,结果显示在一个页面上

这是我一直在尝试的东西,但它不起作用

<?php $catquery = new WP_Query( 'cat=finance-training-seminars&posts_per_page=-1&post-type=dt_portfolio' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>

<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>

<?php endwhile; ?> 
</ul>
<?php wp_reset_postdata(); ?> 

您可以使用列出所有类别,然后为每个类别创建查询,并显示与之相关的帖子,如下所示:

<?php
$categories = get_terms( array( 'taxonomy' => 'category' ) );
foreach( $categories as $cat ) :
    $posts = new WP_Query( array(
        'post_type'     => 'dt_portfolio',
        'showposts'     => -1,
        'tax_query'     => array(
                               array(
                                   'taxonomy' => 'category',
                                   'terms'    => array( $cat->term_id ),
                                   'field'   => 'term_id'
                               )

                          )
    ) ); ?>

    <h3><?php echo $cat->name; ?></h3>

    <ul>
        <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        <?php endwhile; wp_reset_postdata(); ?>
    </ul>

<?php endforeach; ?> 

您可以使用列出所有类别,然后为每个类别创建查询,并显示与之相关的帖子,如下所示:

<?php
$categories = get_terms( array( 'taxonomy' => 'category' ) );
foreach( $categories as $cat ) :
    $posts = new WP_Query( array(
        'post_type'     => 'dt_portfolio',
        'showposts'     => -1,
        'tax_query'     => array(
                               array(
                                   'taxonomy' => 'category',
                                   'terms'    => array( $cat->term_id ),
                                   'field'   => 'term_id'
                               )

                          )
    ) ); ?>

    <h3><?php echo $cat->name; ?></h3>

    <ul>
        <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        <?php endwhile; wp_reset_postdata(); ?>
    </ul>

<?php endforeach; ?> 

试试这个

    <?php 
      $rCateogry = get_the_terms( $post->ID, 'CUSTOM_TAXONOMY_NAME' ); 
      $related = get_posts(array( 'post_type' => 'CUSTOM_POST_TYPE',  'tax_query' => array(array(  'taxonomy' => 'CUSTOM_TAXONOMY_NAME', 'field'    => 'id', 'terms'    => $rCateogry, )), 'showposts' => -1, 'order' => 'DESC', 'post__not_in' => array($post->ID) )); ?>
  <section id="discover">
    <h2>Related Posts</h2>
      <div class="singlepost">
        <?php if( $related ) foreach( $related as $post ) {
          setup_postdata($post); ?>
            <h4><?php the_title(); ?></h4>
                <?php the_excerpt(); ?>
                <?php $cats = array();
                foreach($rCateogry as $c){
                    $cat = get_category( $c );
                    echo $cat->name;
                } ?>
        <?php } 
      wp_reset_postdata(); ?>
      </div>
  </section>

相关职位
将“CUSTOM\u TAXONOMY\u NAME”替换为分类名称,将“CUSTOM\u POST\u TYPE”替换为POST TYPE NAME

    <?php 
      $rCateogry = get_the_terms( $post->ID, 'CUSTOM_TAXONOMY_NAME' ); 
      $related = get_posts(array( 'post_type' => 'CUSTOM_POST_TYPE',  'tax_query' => array(array(  'taxonomy' => 'CUSTOM_TAXONOMY_NAME', 'field'    => 'id', 'terms'    => $rCateogry, )), 'showposts' => -1, 'order' => 'DESC', 'post__not_in' => array($post->ID) )); ?>
  <section id="discover">
    <h2>Related Posts</h2>
      <div class="singlepost">
        <?php if( $related ) foreach( $related as $post ) {
          setup_postdata($post); ?>
            <h4><?php the_title(); ?></h4>
                <?php the_excerpt(); ?>
                <?php $cats = array();
                foreach($rCateogry as $c){
                    $cat = get_category( $c );
                    echo $cat->name;
                } ?>
        <?php } 
      wp_reset_postdata(); ?>
      </div>
  </section>

相关职位

将“CUSTOM_TAXONOMY_NAME”替换为分类名称,将“CUSTOM_POST_TYPE”替换为POST TYPE NAME

感谢您的努力。这只是列出了我所有的类别,但是相关的帖子没有显示出来。仅显示类别名称。@Damon您确定该类别已注册到
dt\u公文包
post type吗?是的,我确定。我得到的是所有列出的分类法,但不是与它们相关的帖子。“while($posts->have_posts())”后面的代码似乎没有任何作用。@Damon我测试了代码,它应该可以正常工作,请用
var_dump
检查
$posts
变量,看看是否有什么东西阻止了帖子的加载,也可以尝试
tax\u query
而不是
中的
category\u,感谢您的努力。这只是列出了我所有的类别,但是相关的帖子没有显示出来。仅显示类别名称。@Damon您确定该类别已注册到
dt\u公文包
post type吗?是的,我确定。我得到的是所有列出的分类法,但不是与它们相关的帖子。“while($posts->have_posts())”后面的代码似乎没有任何作用。@Damon我测试了代码,它应该可以正常工作,请用
var_dump
检查
$posts
变量,看看是否有什么东西阻止了帖子的加载,也可以尝试
tax\u query
而不是
category\u in
谢谢,但仍然没有。替换这行代码,尝试$related=get\u posts(数组('tax\u query'=>array(数组('taxonomy'=>'category','field'=>'id','terms'=>$rCateogry,),),'post\u-type'=>'dt portfolio','numberpost\u'=>1,'not u in'=>数组($post->ID));?>仍然没有。我看到的只是h2标记中的文本谢谢,但仍然没有。请替换这行代码,然后尝试,$related=get_posts(数组('tax_query'=>数组('taxonomy'=>'category','field'=>'ID','terms'=>$rCateogry,),)“post_type”=>“dt_公文包”,“numberposts”=>-1,“post_not_in'=>数组($post->ID));?>仍然是空的。我看到的只是h2标记内的文本