• Resolved indiaberry

    (@indiaberry)


    I would like my blog set up to where I could have random quote from the post show in the side bar. Like I could put this <quote something> test </quote something> around the partial text in the post, where I want to quote in the sidebar.

Viewing 4 replies - 1 through 4 (of 4 total)
  • That might be a bit hard to do. I would suggest using custom fields for this. For example, if you want to show a quote from your posts, you’d need to set up a custom field Key titled “Quote.” Then, give it a Value of your quoted text.

    So, something like this:

    <?php
    if(have_posts()) :
    	while(have_posts()) :
    		the_post();
    		$quote = get_post_custom_values($key = 'Quote');
    		echo $quote[0];
    	endwhile;
    endif;
    ?>

    You could add query_posts('showposts=5'); at the top of that to show your last 5 posts.

    This is totally untested, but it should be something fun to play around with. I do have a tutorial series on using custom fields if you want to learn more about them.

    [Edit]
    Of course, it might be a bit easier just to use the Optional Excerpts, and display it with the_excerpt().

    [Update]
    Okay, it works. I just tested the code above.

    I suppose, if you wanted to get more complicated, you could add several instances of the custom field Key titled “Quote” with different quoted pieces of text. Then, you’d have to use something like this:

    <?php
    if(have_posts()) :
    	while(have_posts()) :
    		the_post();
    		$quote = get_post_custom_values($key = 'Quote');
    		$randomquote = rand(0,count($quote)-1);
    		$quote = $quote[$randomquote];
    		echo $quote;
    	endwhile;
    endif;
    ?>
    Thread Starter indiaberry

    (@indiaberry)

    If I want to show the quote from the posts in the side bar, or in the header, what do I put in the sidebar or header?

    The code I posted.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘random quote from posts’ is closed to new replies.