简体   繁体   中英

Wordpress posts from the same category below post

I'm making a wordpress theme. Right now I'm stuck with something. I want to show the posts from the same category within the loop on a single post. "Aktuelt for deg" is where the posts from the same category should be shown. Live preview. This is my code:

<?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(); ?>

Don't use query_posts . It's not the right tool for what you're trying to do.

Use a WP_Query instead:

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
));

Then, to render it, use this:

<?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; ?>

For anyone else who stumbled upon this question later on:

Joseph Silber's code is almost correct.

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

Should be

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

Because $category is not defined anywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM