根据标签显示相关帖子

时间:2015-04-01 15:56:05

标签: wordpress

我正在一个音乐网站上工作,每个帖子都使用了很多标签和类别。例如,在艺术家的页面上,相关帖子会根据标签显示该艺术家的发布。我尝试使用Wordpress'添加标签。 post_type $ args = array( -

'post_type' => 'releases'

但它没有奏效。

例如,这是完整的代码 -

<div class="relatedposts">
            <h3>Releases by Artist</h3>
            <?php
                $orig_post = $post;
                global $post;
                $tags = wp_get_post_tags($post->ID);

                if ($tags) {
                $tag_ids = array();
                foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                $args=array(
                'post_type' => 'releases',
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>4, // Number of related posts to display.
                'caller_get_posts'=>1
                );

                $my_query = new wp_query( $args );

                while( $my_query->have_posts() ) {
                $my_query->the_post();
                ?>

                <div class="relatedthumb">
                    <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
                    <?php the_title(); ?>
                    </a>
                </div>

                <? }
                }
                $post = $orig_post;
                wp_reset_query();
                ?>
            </div>

我已经关注了很多其他Stackoverflow帖子,但似乎无法获得正确的结果。我哪里错了?!

3 个答案:

答案 0 :(得分:0)

<section class="row related">
    <div class="row related-articles">
        <h2 class="related-title">Related Content</h2>
        <div class="relatedposts">
            <?php
        $orig_post = $post;
        global $post;
        $tags = wp_get_post_tags($post->ID);

        if ($tags) {
            $tag_ids = array();
        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
            $args=array(
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>3, // Number of related posts to display.
                'caller_get_posts'=>1
            );

        $my_query = new wp_query( $args );

        while( $my_query->have_posts() ) {
            $my_query->the_post();
        ?>
            <div class="large-4 medium-4 small-12 columns img-wrap"> <a rel="external" href="<? the_permalink()?>">
                <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') ); ?>
                <img src="<?php echo $url ?>" />
                <div class="story-title-sub">
                    <h2>
                        <?php the_title(); ?>
                    </h2>
                </div>
            </div>
            <?php }
        }
        $post = $orig_post;
        wp_reset_query();
        ?>
        </div>
    </div>
</section>

答案 1 :(得分:0)

您可以尝试使用此代码获取与标记相关的帖子

<?php // related posts based on first tag of current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {

    echo '<h3>Related Posts</h3>';

    $first_tag = $tags[0]->term_id;
    $args = array(
            'tag__in' => array($first_tag),
            'post__not_in' => array($post->ID),
            'showposts' => 7, // how many posts?
            'caller_get_posts' => 1
            );

    $my_query = new WP_Query($args);
    if ($my_query->have_posts()) { ?>

        <ul>

        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

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

        <?php endwhile; ?>

        </ul>

    <?php } ?>
<?php } ?>

答案 2 :(得分:0)

您可以尝试此过程以获取与标签相关的帖子:

<?php
$args = array(
   'posts_per_page' => 3, 
   'post__not_in'   => array( get_the_ID() ), 
   'orderby' => 'count',
);

$cats = wp_get_post_tags( get_the_ID(), 'tag' ); 
$cats_ids = array();  
foreach( $cats as $wpex_related_cat ) {
   $cats_ids[] = $wpex_related_cat->term_id; 
}
if ( ! empty( $cats_ids ) ) {
   $args['tag__in'] = $cats_ids;
}
$wpex_query = new wp_query( $args );
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>

   <a href="<?php the_permalink(); ?>"><li><?php the_title(); ?></li></a>
    enter code here
<?php
endforeach;
wp_reset_postdata();
?>