繁体   English   中英

如何查询自定义帖子类型分类以创建模板页面的分类列表

[英]How to query a custom post type taxonomy to create a taxonomy list for a template page

我正在尝试构建archive-custom.php页面以显示选项卡列表。 每个自定义帖子类型都是与分类法相关联的“消息”,该分类法将它们分组并以“系列”形式组织它们。 分类包含有关每个系列的信息,包括“系列类型”和每个系列的图形。

目标是页面显示“main”类型的所有“系列”的列表,其中包含图形和分类名称。 单击“系列”将带您进入“系列”页面,其中包含所有“消息”的列表

我有一个查询返回我想要的大部分信息,此时它只返回重复项,因为我的查询是错误的。 它返回每个“消息”的“系列”,所以如果有4个“消息”,我将获得4次“系列”。 我知道查询需要改变。

我还有一个问题是返回分类法的名称。 在下面的代码中,我让它返回2种不同的方式 - 一种可以工作,但在链接中返回分类名称,这是不必要的。 另一个只返回“数组”,因为我的语法错误,我在Wordpress codex中找不到一个例子。

我对Wordpress查询不是很熟悉,但我觉得我在这里有一个重复的查询是不必要的。

<?php       
   //this initial query is returning the results for each message instead of returning just each series in the type "main"  
   $args = array(
      'post_type'   => 'messages',
      'post_status' => 'publish',
      //thanks to dmarco for pointing out this should use the meta_query instead
      //not certain how to meta_query the attached taxonomy
      'tax_query'   => array(
         array(
        'taxonomy' => 'cpt_series',
        'field'    => 'series_type',
        'terms'    => 'main'
         )
      )
   );


   $series = new WP_Query( $args );
   if( $series->have_posts() ) :
?>
      <ul>
<?php
      while( $series->have_posts() ) :
         $series->the_post();

         //get the custom taxonomy fields and assign them to var
         //this query is to access the custom post type fields.
         $types = get_terms( array(
            'taxonomy' => 'cpt_series',
            'hide_empty' => true,
         ));

         foreach($types as $type) {
            $image = get_field('series_graphic', 'cpt_series_' . $type->term_id . '' );
            $seriesLink = get_term_link($type->term_id, $taxonomy = 'cpt_series');
            //This title doesn't work - syntax is wrong
            $seriesTitle = wp_get_post_terms('name');

            if ( has_term( $type->term_id, 'cpt_series')) {
               $seriesGraphic = $image['url'];
            }
         }
?>

         <li>
            <a href="<?=$seriesLink?>">
<?php 
               echo '<img src="' . $seriesGraphic . '" />';
               echo $seriesTitle;
               $seriesTitle = the_terms($post->ID, 'sermon_series', '');
?>
            </a>
         </li>
<?php
      endwhile;
      wp_reset_postdata();
?>
      </ul>
<?php
   else :
      esc_html_e( 'No series available!', 'text-domain' );
   endif;           
?>

它非常接近于我想做的事情,再次为每个“消息”获取“系列”而不仅仅是“main”类型中所有“系列”的列表。 很想知道如何正确地获得“名字”。 谢谢。

对于您遇到的第一个问题:

$args = array(
    'post_type'   => 'messages',
    'post_status' => 'publish',
    'tax_query'   => array(
        array(
            'taxonomy' => 'cpt_series',
            'field'    => 'series_type',
            'terms'    => 'main'
        )
    )
);

'field' => 'series_type' - 您在此处根据搜索和选项nameterm_idnameslugterm_taxonomy_id 转到数据库并找到表wp_terms。 你可能会看到这些价值观。 所以我假设这些行应该类似于:

array(
    'taxonomy' => 'cpt_series',
    'field'    => 'slug',
    'terms'    => 'main'
)

您可以针对初始查询尝试此元查询。

$args = array(
     'post_type'   => 'messages',
     'post_status' => 'publish',
     'meta_query' => array(
        array(
         'key' => 'series_type',
         'value' => 'main',
         'compare' => 'IN',

            )

     ) 
 );

通过直接查询分类法发现了如何执行此操作。

$terms = get_terms(array(
   'taxonomy' => 'cpt_series',
   'hide_empty' => true,
   'meta_query' => array(
      array(
         'key'     => 'series_type',
         'value'   => 'main',
         'compare' => 'LIKE'
      ),
   ),
));

if ( ! empty( $terms ) ) {
   echo '<ul>';
   foreach ( $terms as $term ) {
      $image = get_field('series_graphic', 'cpt_series_' . $term->term_id . '' );
      $seriesLink = get_term_link($term->term_id, $taxonomy = 'cpt_series');
      $seriesGraphic = $image['url'];
   ?>
      <li>
         <a href="<?=$seriesLink?>">
            <img src="<?=$seriesGraphic?>" />
            <?=$term->name?>
         </a>
      </li>                     
 <?php                  
   }
   echo '</ul>';
} else {
   echo 'No term found.';
}

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM