Forums

Random posts in Gabfire's free WordPress Magazine theme (5 posts)

  1. truthlighthouse
    Member
    Posted 2 years ago #

    Just a quick edit. I am using WordPress version 2.9.2. It did not have that as a choice in the drop down.

    I am looking to have random posts on my home page. Specifically I am talking about the center column which is titled "recent posts". I have tried a few things that have not worked and I was hoping that someone might be able to help.

    I should also point out that I am completely illiterate when it comes to PHP. I am able to fumble around in there and usually figure things out with some things out but I need dumbed down guidance.

    I have tried the following items.

    1) I installed the plugin Random Posts by Robert Marsh and inserted the correct code <?php random_posts(); ?> in the index.php page. It correctly started producing random posts on my home page in the center column. The problem is it did not do anything with the posts that are showing now. It actually added another section with simple bullet points and randomized those.
    2) I tried inserting the following code &orderby=rand in there as well. That was based on help I received for another theme I am working on and that actually worked...for that theme. But on this one, it simply does not do anything. I added it to:
    <?php query_posts('cat=13,14,15,16,17,18,19,20,21,29,30,22,23,24,25,28,26,27&showposts=5'); ?>
    which made <?php query_posts('cat=13,14,15,16,17,18,19,20,21,29,30,22,23,24,25,28,26,27&showposts=5&orderby=rand'); ?>
    3) I tried another long drawn out bit that I found on another website but it created parse errors and so I had to nix that.

    If anyone could help, that would be awesome! Below is the code from index.php:

    <!-- Middle Column -->
    		<div id="midcolumn">
    			<h2>Recent Posts</h2>
    			<!-- Loop for latest news -->
    			<?php $oddentry = 'class="gray" '; ?>
    			<?php query_posts('cat=13,14,15,16,17,18,19,20,21,29,30,22,23,24,25,28,26,27&showposts=5'); ?>
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    			<div <?php echo $oddentry; ?>>
    				<div class="midcolumnpost">
    					<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    					<?php the_excerpt_reloaded(30, '<img><a>', 'content', false, 'More...', true);?>
    					<div class="details">
    						 | <?php comments_popup_link(__('Leave comment'), __('1 Comment'), __('% Comments'));?> | <a href="<?php echo get_permalink(); ?>" title="Read More">Read More</a>
    					</div>
    				</div>
    			</div>
    			<?php /* Changes every other post to a different class */	$oddentry = ( empty( $oddentry ) ) ? 'class="gray" ' : ''; ?>
    			<!-- End of Loop for middle column -->

    My website in question is http://www.truthlighthouse.com

    Please try to make the instructions as simple and complete as possible. I apologize but I really don't know much about the inner workings of PHP.

    Thanks so much!

    [moderated--bump removed. Please refrain from bumping as per Forum Rules]

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Don't have as many categories as you to test, but this seems to work fine for me:

    <?php
    $args=array(
      'category__in' => array(1,15,3),
      'orderby'=>'rand',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 5,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Please note I deleted your duplicate post on this subject.

  3. truthlighthouse
    Member
    Posted 2 years ago #

    Thanks so much for your help! I appreciate it. I apologize about the bump. I did not know about that rule.

    As for the code you suggested, where exactly would I put that? My guess is I would replace the entire portion that pertains to the middle column in my index.php page. However, I then thought it could be that I need to replace only a portion of it. In fact, I was thinking that the part that starts with

    <?php query_posts('cat=13,14,15,16,17,18,19,20,21,29,30,22,23,24,25,28,26,27&showposts=5'); ?>

    and that ends with

    <div class="details">

    However, I am not certain so I was hoping you might be able to help a bit further. I was concerned about going in there and changing things too much before having a general idea.

    Thanks again!

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    You are correct.

  5. truthlighthouse
    Member
    Posted 2 years ago #

    I apologize but I am still unable to get that to work. I may be still doing it wrong but I tried a few different routes. I tried applying it between the lines I mentioned above as well as a few different areas. One thing I notice is if I replace the part above, a variable $oddentry is assigned in the old code and never gets used if I replace the part that I indicated above that I would replace. If possible could you take a look at this? Thanks so much for your help. It is greatly appreciated. Below is what I tried and I got a parse error. The error states "Parse error: syntax error, unexpected T_ENDWHILE in /html/wp-content/themes/Wordpress_Magazine/index.php on line 73". Also, for now while I play with it, I am changing this up on a dummy website which is different than the one above.

    <<!-- Middle Column -->
    		<div id="midcolumn">
    			<h2>Latest News</h2>
    			<!-- Loop for latest news -->
    			<?php $oddentry = 'class="gray" '; ?>
    			<?php
    $args=array(
      'category__in' => array(1,15,3),
      'orderby'=>'rand',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 5,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    
    						 | <?php comments_popup_link(__('Leave comment'), __('1 Comment'), __('% Comments'));?> | <a href="<?php echo get_permalink(); ?>" title="Read More">Read More</a>
    					</div>
    				</div>
    			</div>
    			<?php /* Changes every other post to a different class */	$oddentry = ( empty( $oddentry ) ) ? 'class="gray" ' : ''; ?>
    			<!-- End of Loop for middle column -->
    			<?php endwhile; else : ?>
    			<?php endif; ?>
    		</div>
    		<!-- /Middle Column -->

Topic Closed

This topic has been closed to new replies.

About this Topic