簡體   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