Wordpress帖子来自帖子下方的同一类别


Wordpress posts from the same category below post

我正在制作一个wordpress主题。现在我陷入了困境。我想在单个帖子的循环中显示来自同一类别的帖子。"Aktuelt for deg"是来自同一类别的帖子应该显示的地方。实时预览。这是我的代码:

<?php get_header(); ?>
<div id="hold" class="clearfix">
    <div id="left">
        <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
        <div class="entry">
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <div class="author">Skrevet av <?php the_author(); ?></div>
            <?php the_content(); ?>
        </div>
        <div class="comment-template">
            <?php comments_template(); ?>
        </div>
    </div>
<div id="right">
            <div class="search">
                <form  action="<?php echo home_url( '/' ); ?>" method="get">
                                    <input type="text" value="Skriv her for å søke.." name="s" id="s" onblur="if (this.value == '') {this.value = 'Skriv her for å søke..';}"
                                    onfocus="if (this.value == 'Skriv her for å søke..') {this.value = '';}">
                                </form>
                                <script type="text/javascript">
                                    $(".search").keypress(function(ev){
                                        if (ev.keyCode == 13) {
                                            $("form")[0].submit();
                                        }
                                    });
                                </script>
            </div>
            <div class="box">
                <div class="heading">
                    <h1>Aktuelt for deg</h1>
                </div>​
                <ul>
                    <?php query_posts('posts_per_page=5' . '&orderby=rand'); 
                    while ( have_posts() ) : the_post();
                        echo '<li><div class="borderline"><a href="';
                        the_permalink();
                        echo '">';
                        the_title();
                        echo '</a></div><author>Skrevet av ';
                        the_author();
                        echo '</author></li>';
                    endwhile;
                    // Reset Query
                    wp_reset_query();
                    ?>
                </ul>
            </div>
        </div>
    </div>
    <?php endwhile; ?>
    <?php endif; ?>
<?php get_footer(); ?>

不要使用query_posts。这不是适合你尝试做什么的工具。

改为使用WP_Query

global $post;
$current_category = get_the_category();
$same_category = new WP_Query(array(
    'cat'            => $category[0]->cat_ID,
    'post__not_in'   => array($post->ID),
    'orderby'        => 'rand',
    'posts_per_page' => 5
));

然后,要渲染它,请使用以下命令:

<?php while ( $same_category->have_posts() ) : $same_category->the_post(); ?>
    <li>
        <div class="borderline">
            <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
            </a>
        </div>
        <author>Skrevet av <?php the_author(); ?></author>
    </li>
<?php endwhile; ?>

对于后来偶然发现这个问题的其他人:

Joseph Silber的代码几乎是正确的。

'cat'            => $category[0]->cat_ID,

应该是

'cat'            => $current_category[0]->cat_ID,

因为$category没有在任何地方定义。