如何显示同一类别的相关帖子?

时间:2011-10-02 作者:BBee

是否可以显示与当前帖子类别相同的相关帖子?

4 个回复
最合适的回答,由SO网友:Michael 整理而成

一种可能性:

$related = get_posts( 
    array( 
        \'category__in\' => wp_get_post_categories( $post->ID ), 
        \'numberposts\'  => 5, 
        \'post__not_in\' => array( $post->ID ) 
    ) 
);

if( $related ) { 
    foreach( $related as $post ) {
        setup_postdata($post);
        /*whatever you want to output*/
    }
    wp_reset_postdata();
}
参考号:

根据重新编写的答案WP_Query():

$related = new WP_Query(
    array(
        \'category__in\'   => wp_get_post_categories( $post->ID ),
        \'posts_per_page\' => 5,
        \'post__not_in\'   => array( $post->ID )
    )
);

if( $related->have_posts() ) { 
    while( $related->have_posts() ) { 
        $related->the_post(); 
        /*whatever you want to output*/
    }
    wp_reset_postdata();
}

SO网友:Lawrence Oputa

这里是另一个干净且非常灵活的选项:

将此代码放入函数中。php文件

function example_cats_related_post() {

    $post_id = get_the_ID();
    $cat_ids = array();
    $categories = get_the_category( $post_id );

    if(!empty($categories) && is_wp_error($categories)):
        foreach ($categories as $category):
            array_push($cat_ids, $category->term_id);
        endforeach;
    endif;

    $current_post_type = get_post_type($post_id);
    $query_args = array( 

        \'category__in\'   => $cat_ids,
        \'post_type\'      => $current_post_type,
        \'post__not_in\'    => array($post_id),
        \'posts_per_page\'  => \'3\'


     );

    $related_cats_post = new WP_Query( $query_args );

    if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?>
            <ul>
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                    <?php the_content(); ?>
                </li>
            </ul>
        <?php endwhile;

        // Restore original Post Data
        wp_reset_postdata();
     endif;

}
现在,您可以使用以下命令在站点中的任意位置调用该函数:

<?php example_cats_related_post() ?>
您可能需要删除列表元素或根据需要设置其样式。

*编辑-您可以将此更改:post\\u not\\u in更改为查询中的此post\\u not\\u in

SO网友:almhdy

您可以使用此代码从同一类别获取相关帖子

$args = array(
                \'category__in\' => wp_get_post_categories( get_queried_object_id() ),
                \'posts_per_page\' => 5,
                \'orderby\'       => \'rand\',
                \'post__not_in\' => array( get_queried_object_id() )
                );
    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) : ?>

        <ul class="">
        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

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

        <?php endwhile; ?>
        <!-- end of the loop -->
        </ul>

        <?php wp_reset_postdata(); ?>

     <?php endif; ?>
并使用此代码从相同标记获取相关帖子

$tags = wp_get_post_terms( get_queried_object_id(), \'post_tag\', [\'fields\' => \'ids\'] );
    $args = [
        \'post__not_in\'        => array( get_queried_object_id() ),
        \'posts_per_page\'      => 5,
        \'orderby\'             => \'rand\',
        \'tax_query\' => [
            [
                \'taxonomy\' => \'post_tag\',
                \'terms\'    => $tags
            ]
        ]
    ];
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) : ?>

        <ul class="">
        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

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

        <?php endwhile; ?>
        <!-- end of the loop -->
        </ul>

        <?php wp_reset_postdata(); ?>

    <?php endif; ?>

SO网友:Felix Eve

这个答案确保了相关帖子是按照匹配的标签数量排序的。

例如,如果一篇文章有3个标签,而另一篇文章有完全相同的3个标签,那么它应该显示在列表的顶部。二级排序应按发布日期进行,以便于更新内容。

/**
 * Select content with common tags.
 * Sort so content with multiple matching tags are at the top.
 * Secondary sort on most recent content first.
 *
 * @param $post_id
 * @param int $limit
 * @return array
 */
function related_posts($post_id, $limit = 5) {

    global $wpdb;

    $query  = "SELECT TOP %d x.object_id as ID
FROM (
SELECT TOP 10 tr1.object_id, COUNT(tr1.term_taxonomy_id) AS common_tag_count
FROM {$wpdb->term_relationships} AS tr1
INNER JOIN {$wpdb->term_relationships} AS tr2 ON tr1.term_taxonomy_id = tr2.term_taxonomy_id
WHERE tr2.object_id = %d
GROUP BY tr1.object_id
HAVING tr1.object_id != %d
ORDER BY COUNT(tr1.term_taxonomy_id) DESC
) x
INNER JOIN {$wpdb->posts} p ON p.ID = x.object_id
ORDER BY common_tag_count DESC, p.post_date DESC;";

    $query = $wpdb->prepare($query, $limit, $post_id, $post_id);
    $ids = $wpdb->get_col($query);
    $posts = [];
    foreach($ids as $id)  {
        $posts[] = get_post($id);
    }

    return $posts;

}
这里的内部查询是选择标签最匹配的内容,然后外部查询只是用于按发布日期进行二次排序。

注意:此查询是为SQL Server编写的,因此某些语法可能需要更新(例如,TOP vs LIMIT)。

结束

相关推荐

Show all posts in category

我试图显示每个类别中的所有帖子。我一直在四处搜索,似乎我需要slug 或term_id 如下面的代码所示。<?php query_posts(\'category_name=MyCategory&showposts=9999\'); ?> 我目前正在编辑archive.php 如何获取slug 名称谢谢