Related posts with WP_Query

时间:2015-07-02 作者:Alex P.

我一直在查询相关帖子。我需要相关的职位,以显示任何深度从主家长。我的网站上有以下结构类别:

- Category level 1
-- Category level 2
--- Category level 3
*---- Post in level 3*
--- Category level 3
*--- Post in level 3*
-- Category level 2
-- Category level 2
当我们看到*---- Post in level 3* 我们必须从*-- Category level 2* ie所有包含此级别的帖子,包括child。

这是我的代码:

<div class="related_goods">
<span>Related posts:</span>
    <?php
    $categories = get_the_category($post->ID);
    if ($categories) {
    $category_ids = array();
    foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    $args=array(
    \'category__in\' => $category_ids,
    \'post__not_in\' => array($post->ID),
    \'showposts\'=>3,
    \'orderby\'=>title,
    \'post_parent\'=> array(),
    \'caller_get_posts\'=>1);
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {

    while ($my_query->have_posts()) {
    $my_query->the_post();
    ?>
    <div class="block-related-item">
        <div class="preview-wrap">
            <div class="preview">
                <? $thumb_url = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
                <a href="<?php the_permalink() ?>"><? the_post_thumbnail(\'category_thumbs\'); ?>
                </a>
            </div>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </div>          
    </div>
    <?php
    }
    }
    wp_reset_query();
    }
    ?>
</div>
如何修改它以获得上述效果?提前多谢!

1 个回复
SO网友:Pieter Goosen

parent 这里是你的答案。每个术语在其parent 所有物该值是一个整数值,表示其父项的项id。所有顶级术语的值均为0 这仅仅意味着这是一个顶级术语

首先,我们需要使用父项和get_the_category. 我们将跳过0 价值观一旦有了ID数组,我们将获得所有唯一的值,并将ID数组传递给tax_query 为了保存多个查询

以下代码未经测试,需要PHP 5.4+)

function get_related_category_posts()
{
    // Check if we are on a single page, if not, return false
    if ( !is_single() )
        return false;

    // Get the current post id
    $post_id = get_queried_object_id();

    // Get the post categories
    $categories = get_the_category( $post_id );

    // Lets build our array
    // If we don\'t have categories, bail
    if ( !$categories )
        return false;

    foreach ( $categories as $category ) {
        if ( $category->parent == 0 ) {
            $term_ids[] = $category->term_id;
        } else {
            $term_ids[] = $category->parent;
            $term_ids[] = $category->term_id;
        }
    }

    // Remove duplicate values from the array
    $unique_array = array_unique( $term_ids );

    // Lets build our query
    $args = [
        \'post__not_in\' => [$post_id],
        \'posts_per_page\' => 3, // Note: showposts is depreciated in favor of posts_per_page
        \'ignore_sticky_posts\' => 1, // Note: caller_get_posts is depreciated
        \'orderby\' => \'title\',
        \'no_found_rows\' => true, // Skip pagination, makes the query faster
        \'tax_query\' => [
            [
                \'taxonomy\' => \'category\',
                \'terms\' => $unique_array,
                \'include_children\' => false,
            ],
        ],
    ];
    $q = new WP_Query( $args );
    return $q;
}
然后,您可以在单个贴子页面中使用以下代码

$q = get_related_category_posts();
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();

        // Your loop

    }
    wp_reset_postdata();
}

结束

相关推荐

向GET_CATEGORIES下拉列表添加自定义选项

我有一个下拉菜单,用于在我正在处理的小部件中选择类别。一切正常,选项保存在数据库中。我现在要做的是添加一个空白选项,而不是在单击“保存”时自动设置。在这种情况下,用户可能不想设置类别。 $this->categories = get_categories(); foreach ( $this->categories as $cat ) { $selected = ( $cat->term_id == esc_att