【问题标题】:Show all post in taxonomy category from certain id or slug显示来自特定 id 或 slug 的分类类别中的所有帖子
【发布时间】:2015-10-16 04:02:06
【问题描述】:

我有一个名为“电子”的自定义帖子类型,其分类类别称为“计算机、手机、笔记本电脑......等等”

在 taxonomy.php 模板中,我设法使用此代码获取了分类名称、slug 和 id。

<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
echo $term->name; // will show the name
echo $term->slug; // will show the slug
echo $term->term_id; // will show the id
?>

所以如果我在计算机分类页面中,我会得到'Computers computers 11'

无论是 pf 每次生成的值,我如何根据我所在的“分类页面”生成帖子。例如。计算机分类页面中带有“计算机”标签的帖子,电话分类页面中带有手机标签的帖子......等等。

类似这样的东西,但对于分类法......

<?php
$catquery = new WP_Query( 'cat=3' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

【问题讨论】:

  • 只需将自定义查询替换为默认循环,该循环将显示作为主查询返回的正确帖子

标签: php wordpress custom-taxonomy


【解决方案1】:

您需要使用 tax_query 来获取具有特定分类术语的帖子,方法如下:

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'electronics',
            'field' => 'slug',
            'terms' => $term->slug
        )
    )
);
$catquery = get_posts( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多