繁体   English   中英

Wordpress WP_Query-基于分类的相关文章

[英]Wordpress WP_Query - Related posts based on taxonomy

我正在尝试构建WP_Query,它会循环浏览来自某个自定义帖子类型(在本例中为当前产品)的帖子,并返回所有标有相同分类法(在这种情况下,与正在显示的帖子)

这是我到目前为止的内容:

<ul>
<?php query_posts('post_type=current-products');
if (have_posts()) : while (have_posts()) : the_post(); ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>

尝试这个:

$post_terms = wp_get_object_terms($post->ID, 'your-taxonomy', array('fields'=>'ids'));

$args = array(
    'post_type' => 'current-products',
    'tax_query' => array(
        array(
            'taxonomy' => 'your-taxonomy',
            'field' => 'id',
            'terms' => $post_terms
        )
    )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
        echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        ?>
                    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
    }
        echo '</ul>';
} else {
    // no posts found
}
wp_reset_postdata();

此时应更换的出现your-taxonomy与您的分类类型,以便此代码工作的代码。

暂无
暂无

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

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