本文介绍了Wordpress - 在自定义分类中获取帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个星期以来,我一直在为 wordpress 问题苦苦挣扎,但我无法弄清楚.

Ive been struggling with a wordpress issue for a few weeks now and i just cant figure it out.

我创建了一个名为cpt_used"的自定义帖子类型,在该自定义帖子类型中我创建了一个名为tax_used"的自定义分类法,这是一个类别列表

I have created a custom post type called 'cpt_used', in that custom post type i have created a custom taxonomy called 'tax_used', which is a categories list

我需要做的是显示属于每个自定义分类法的所有帖子,但我无法弄清楚.

What i need to do is show all posts that belong to each custom taxonomy and i just cant figure it.

我目前的代码如下,每个类别中有多个帖子,但它只是不显示任何内容

the code i have at the moment is as follows, there are multiple posts in each of the categories, but its just not displaying anything

$args = array(
    'orderby' => 'name',
    'hide_empty' => 0,
    'taxonomy' => 'tax_used'
);
$categories = get_categories($args);

foreach( $categories as $category ) {

    $newargs = array(
        'category_name' => $category->slug,
        'taxonomy' => 'tax_used',
        'term' => 'cpt_used'
    );

    query_posts( $newargs );
    if (have_posts()) :
        while (have_posts()) : the_post();
            the_title();
        endwhile;
    endif;

}

推荐答案

$newargs 一团糟.试试这个:

$newargs are totally messed. try this:

$newargs = array(
 'post_type' => 'cpt_used',
 'tax_query' => array(
  array(
   'taxonomy' => 'tax_used',
   'field' => 'slug',
   'terms' => $category->slug
  )
 )
);

并记住 print_r() 有时会返回值以在开始迭代之前检查它是否正是您想要的 ;)

and remember print_r() sometimes returned values to check if it is exactly what you want before you start to iterate it ;)

这篇关于Wordpress - 在自定义分类中获取帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:34