【问题标题】:WP query both post type and categoryWP查询帖子类型和类别
【发布时间】:2014-06-16 16:09:37
【问题描述】:

我的 WP 查询有问题,我有两种自定义帖子类型(电影和书籍),它们依赖于相同的类别,例如动作、戏剧等。

用户的步骤是:

电影还是书籍? (选择书籍) 什么类别的书? (选择:行动) 在动作类别中显示书籍。

我已经来了这么远:

在 category.php 里面我有这个 if 语句:

if (have_posts()){
 the_post();
 if('books' == get_post_type( $post )) {
    rewind_posts();
    get_template_part('category-hom-regler');
    exit();
 }
rewind_posts();
}
 ?>

加载另一个页面模板。但问题是书籍和电影都使用相同的类别,因此书籍出现在电影等之下。

如何让 wordpress 单独显示?

【问题讨论】:

  • wp_query 有效!但是现在我遇到了一个问题,即 if 语句不起作用并将所有帖子类型发送到“category-hom-regler”。我尝试将代码更改为:<?php if (have_posts()){ $post = the_post(); if('hom_regler' == get_post_type( $post )) { rewind_posts(); get_template_part('category-hom-regler'); exit(); } rewind_posts(); } ?>,但没有帮助

标签: php wordpress custom-post-type


【解决方案1】:

解决方案是创建将自定义页面模板和类别与子类别分开。

在自定义页面模板中使用此代码:

if(count($customPostTaxonomies) > 0)
  {
     foreach($customPostTaxonomies as $tax)
     {
     $args = array(
          'orderby' => 'name',
          'show_count' => 0,
          'pad_counts' => 0,
          'hierarchical' => 1,
          'child_of' => 63,
          'taxonomy' => $tax,
          'title_li' => ''
        );

     wp_list_categories( $args );
   }
}

“child_of”是类别的 ID。然后,您在我的示例“电影”和“书籍”中创建一个主类别,并在“动作”、“戏剧”等示例中为您的类别创建子类别。

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2021-06-07
    相关资源
    最近更新 更多