【问题标题】:How can I get posts from only one custom post category where there are many categories?如何仅从一个有多个类别的自定义帖子类别中获取帖子?
【发布时间】:2019-03-09 06:04:31
【问题描述】:

我有带有类别的投资组合自定义帖子类型。我想使用一个模板来获取仅一个类别的投资组合帖子。

$args = array( 
     'post_type' => 'portfolio',
     'posts_per_page' => -1,
     'category_name' => 'casino'
  );

如果我将 category_name 与 'casino' 之类的类别标签一起使用,我必须为每个类别创建模板,但我不想要它。我只想使用一个模板,并且类别 slug 将动态放置在 category_name 值上。如何创建它?

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案1】:

    为自定义帖子创建类别模板: 获取类别对象使用;

    <?php $term = get_queried_object(); ?>
    

    然后您可以将此作为页面标题回显,以便您的用户知道他们正在浏览某个类别,然后在自定义查询参数中使用它。

    <?php echo $term->name; ?> // shows category name
    
        $args = array(
            'post_type' => 'portfolio',
            'category_name' => $term->slug
            );
    

    【讨论】:

    • 谢谢,但我想要许多类别中的一个类别slug,可以吗?
    • 重新阅读你最初的问题,你现在问的是完全不同的问题。类别 slug 对于每个类别都是唯一的。除非您创建一个包含多个按类别过滤的自定义循环查询的模板。
    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多