帮助显示相关类别

时间:2017-01-05 作者:MikeC

如何显示相关类别,而不是显示相关标记的后续操作。如果您有任何帮助,我们将不胜感激。提前感谢

if(!function_exists(\'solstice_related_post\')) {
    function solstice_related_post() {
        global $post;
        $tags = wp_get_post_tags($post->ID);

        if(!empty($tags) && is_array($tags)):
            $simlar_tag = $tags[0]->term_id;

            ?>

            <div class="related-posts">
                <h6><?php esc_html_e(\'YOU MIGHT ALSO LIKE\', \'solstice-theme\'); ?></h6>
                <div class="row">
                    <?php
                    $args = array(
                        \'tag__in\'             => array($simlar_tag),
                        \'post__not_in\'        => array($post->ID),
                        \'posts_per_page\'      => 3,
                        \'meta_query\'          => array(array(\'key\' => \'_thumbnail_id\', \'compare\' => \'EXISTS\')),
                        \'ignore_sticky_posts\' => 1,
                    );
                    $re_query = new WP_Query($args);
                    while ($re_query->have_posts()) : $re_query->the_post();
                        ?>
                        <article <?php post_class(\'blog-post col-md-4\'); ?>>
                            <header>
                                <figure>
                                    <?php the_post_thumbnail(\'solstice-small\'); ?>
                                </figure>
                                <h3><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h3>
                                <div class="meta">
                                    <span><?php echo get_the_category_list( \' , \', \'solstice-theme\' );?></span>
                                    <span><time datetime="<?php the_time(\'Y-m-d\'); ?>"><?php the_time(\'F d, Y\'); ?></time></span>
                                </div><!-- /meta -->
                            </header>
                        </article>
                    <?php endwhile; wp_reset_postdata(); ?>
                </div><!-- /row -->
            </div><!-- /related-posts -->
            <?php
        endif;
    }
}

2 个回复
SO网友:Benoti

代替wp_get_post_tags($post->ID);具有

wp_get_post_terms($post->ID);
可以找到所有参数和详细信息here

并更改$args参数

$args = array(
    \'category__in\'        => array($simlar_tag),
    \'post__not_in\'        => array($post->ID),
    \'posts_per_page\'      => 3,
    \'meta_query\'          => array(array(\'key\' => \'_thumbnail_id\', \'compare\' => \'EXISTS\')),
    \'ignore_sticky_posts\' => 1,
  );
希望它能起作用!

SO网友:MikeC

感谢您的回复!但是,更改后没有相关的帖子显示。有什么想法吗?

以下是我将代码更改为的内容:

if(!function_exists(\'solstice_related_post\')) {
函数solstice\\u related\\u post(){全局$post;$tags=wp\\u get\\u post\\u terms($post->ID);

if(!empty($tags) && is_array($tags)):
  $simlar_tag = $tags[0]->term_id;

?>

<div class="related-posts">
  <h6><?php esc_html_e(\'YOU MIGHT ALSO LIKE\', \'solstice-theme\'); ?></h6>
  <div class="row">
    <?php
    $args = array(
\'category__in\'        => array($simlar_tag),
\'post__not_in\'        => array($post->ID),
\'posts_per_page\'      => 3,
\'meta_query\'          => array(array(\'key\' => \'_thumbnail_id\', \'compare\' => \'EXISTS\')),
\'ignore_sticky_posts\' => 1,
)$re\\u query=新的WP\\u查询($args);while($re\\u query->have\\u posts()):$re\\u query->the\\u post();?>>

“>

”>