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

Answers


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

Need Your Help

making a custom calendar like layout programicaly

java android

I am developing an app which basically should download a list of events from a website and then views them in a in a calendar like layout. I already covered the first part. But I don't have an idea...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.