PHP相关帖子按类别描述?

时间:2015-01-09 08:49:50

标签: php wordpress

我有安装wordress。现在我想添加带有一些描述的相关帖子。 所以我创造了这个。

<div class="related">
    <h3>Related Articles</h3>
    <?php
        $orig_post = $post;
        global $post;
        $tags = wp_get_post_categories($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'=>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>

现在,当我将此代码粘贴到我的wordpress博客时,它无法正常工作,请帮忙。 这段代码有什么问题。

1 个答案:

答案 0 :(得分:0)

尝试在您想要显示相关帖子的循环之后在single.php中添加此代码

<?php

    $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 4, 'post__not_in' => array($post->ID) ) );
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>
     <ul> 
            <li>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                <?php the_content('Read the rest of this entry &raquo;'); ?>
            </li>
        </ul>   
    <?php }
    wp_reset_postdata(); ?>