• Resolved Eelco Deuling

    (@eelco-deuling)


    Dear everybody,
    For a custom theme I want to insert a random excerpt (wrapped in quotes).

    I currently use the following code for this (but I have tried some variations):

    <?php
    query_posts(array('orderby' => 'rand', 'showposts' => 1));
    if (have_posts()) :
    while (have_posts()) : the_post();
    ?>
    <q><?php the_excerpt_rss(); ?></q>
    <div class="read_more">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read more</a>
    </div>
    <?php endwhile; ?>
    <?php endif;
    ?>

    This does works, but if I insert this code before the Loop, the content of the page only displays a post, no matter what page I want to view.
    If I insert the code after the Loop everything works as expected, but to place it with css I have to use position: absolute;. The quote is supposed to be beneath the main menu, so if this grows it will do so beneath the quote.

    Does someone knows what is wrong with my code?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Caveats
    query_posts() is only one way amongst many to query the database and generate a list of posts. Before deciding to use query_posts(), be sure to understand the drawbacks.

    Alters Main Loop
    query_posts() is meant for altering the main loop. It does so by replacing the query used to generate the main loop content. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered – this may or may not be the intended result.

    Secondary Loops
    To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts().

    If you must use query_posts(), make sure you call wp_reset_query() after you’re done.

    I think that explains your problem.

    Thread Starter Eelco Deuling

    (@eelco-deuling)

    Thank you janet4now,

    I am still struggling with WordPress (this is my third one after six years), and this is a better explanation than I had encountered elsewhere. It does explains my problem, so this topic can be marked as resolved.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Random post excerpt in sidebar.’ is closed to new replies.