How to display two random-post sections that are each under their own category

The relevant code is here:

query_posts("cat=25&orderby=DESCd&showposts=3&$randomArticlesNum&caller_get_posts=1");

That is the query that is pulling the posts, and that is what you want to alter.

Your previous theme builder also did something that should never be done– used query_posts(). Also, both showposts and caller_get_posts have been replaced. That is very old code or your previous programer did not keep up with changes to the core. Let’s clean this up:

$args = array(
  'cat' => 25, 
  'orderby' => 'DESC',
  'ignore_sticky_posts' => true, // used to be caller_get_posts
  'posts_per_page' => 3 // used to be showposts
);
$qry = new WP_Query($args);
if ($qry->have_posts()) {
  while ($qry->have_posts() ) { 
    $width = 40;
    $height = 40;
    $classtext="no-border";
    $titletext = get_the_title();
    $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext);
    $thumb = $thumbnail["thumb"]; ?>
    <div class="random"><?php 
      $width = 40;
      if($thumb != '') { ?>
        <div class="random-image"> "><?php 
          print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
        </div><?php 
      } 
      // something is broken here. the start of the following tag is missing
      ?>" rel="bookmark" title="<?php printf(esc_attr__('Permanent Link to %s','EarthlyTouch'), the_title()) ?>"><?php 
        truncate_title(40);
        truncate_post(50); ?>
    </div>
    <!-- end .random --><?php
  }
}

I swapped query_posts() for a new WP_Query object, and I reordered for readability, eliminating as much PHP tag SPAM as I could at a quick once-over.

I don’t know what $randomArticlesNum was supposed to do but looking at the argument list to query_posts() I don’t see how it could have actually done anything.

You want to do the same thing again for your next Loop but change the cat argument. As is, both Loops have cat=25 so there is no way that could have worked either.

You also have a broken tag in there. See the comments to the code.

One other trick: The code looks to be identical for both Loops but you are maintaining the same code in two places. That is a headache and is prone to error. Move most of the Loop code to another file and load it via get_template_part():

$args = array(
  'cat' => 25, 
  'orderby' => 'DESC',
  'ignore_sticky_posts' => true, // used to be caller_get_posts
  'posts_per_page' => 3 // used to be showposts
);
$qry = new WP_Query($args);
if ($qry->have_posts()) {
  while ($qry->have_posts() ) { 
    get_template_part('content','blocks');
  }
}

That will load a file called content-blocks.php from the theme root– the same directory as the themes’ stylesheet and functions.php.


I took a look at the edits you made and you didn’t quite get it all right. Here is some code with notes:

// $randomArticlesNum does nothing, as noted previously. I am not even sure what it was supposed to do.    
// You can remove it
$randomArticlesNum = (int) get_option('earthlytouch_randomposts_num'); 
$args = array(
  // use one or the other, but not both, of the following and delete the one you don't use
  // Your original code use `cat` and the ID
  'cat' => 123, // 'cat' needs to be an ID-- a number-- not a word, 
  'category_name' => 'whats', // for a word, use 'category_name' and quote the value
  'orderby' => 'DESC',
  'ignore_sticky_posts' => true, // used to be caller_get_posts
  'posts_per_page' => 3 // used to be showposts
);
$query = new WP_Query($args);
if ($query->have_posts()) {
  while ($query->have_posts() ) { 
    get_template_part('content','blocks');
  }
} // you were missing the PHP closing tag --> ?> 

The same errors are in both Loops. Also, after the second one, you have a spurious closing bracket:

<!-- end .home-squares -->

      <?php }; ?>

Remove it:

<!-- end .home-squares -->

Also, get yourself a decent code editor– something like Textwrangler (on a Mac and I’m guessing you are :)) or Notepad++.


I edited and reformatted your template:

<?php
get_header(); ?>

<div id="containerHOME">
  <div id="left-div">
  <!--Begin Sidebar-->
    <div id="left-inside"><?php 
      if (get_option('earthlytouch_featured') == 'on') {
        get_template_part('includes/featured'); 
      } ?>
      <div class="clear">&nbsp;</div>
      <div class="home-squares">
        <div class="home-headings"><?php 
          esc_html_e('Whats New?','EarthlyTouch'); ?>
        </div><?php 
        $args = array(
          'category_name' => 'whats', 
          'orderby' => 'DESC',
          'ignore_sticky_posts' => true,
          'posts_per_page' => 3 
        );
        $qry = new WP_Query($args);
        if ($qry->have_posts()) {
          while ($qry->have_posts() ) { 
            get_template_part('content','blocks');
          }
        } ?>
        <!-- end .home-squares -->
        <div class="home-squares">
          <div class="home-headings">
            <?php esc_html_e('Recipes','EarthlyTouch'); ?>
          </div>
          <?php 
          $args = array(
            'category_name' => 'recipes', 
            'orderby' => 'DESC',
            'ignore_sticky_posts' => true, 
            'posts_per_page' => 3 
          );
          $query = new WP_Query($args);
          if ($query->have_posts()) {
            while ($query->have_posts() ) { 
              get_template_part('content','blocks');
            }
          } ?>
          <!-- end .home-squares -->
          <!-- movie -->
          <div class="movie"> <a href="http://www.youtube.com/embed/tojlpK9zFPs"> <img src="images/video-front.jpg" width="530" height="299" alt="video" class="popmake-2673" /></a> </div>
        </div>
        <!-- end movie --> 
        <!-- end #left-inside --> 

        <!--Begin Sidebar--><?php 
        get_sidebar(); ?>
        <!--End Sidebar-->
        <div id="sup-hold">
          <div id="supplementary" style="background-color:#FFFFFF;"><?php 
            if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Header Widget Area')) : endif; ?>
          </div>
          <div id="mc-mobilecause-widget"> <a href="https://www.mobilecause.com/feature/subscription-widget" id="powered-by-mobilecause">Mobile messaging powered by Mobilecause</a><script>!function(d,s){var s=d.createElement("script"),h=(document.head||d.getElementsByTagName('head')[0]);s.src="https://app.mobilecause.com/public/messaging_widgets/opvd3d/source";h.appendChild(s);}(document);</script> 
          </div>
          <div id="footer"> Copyright &copy; <?php echo date("Y"); ?> JustOneTree.org All Rights Reserved | <a href="https://wordpress.stackexchange.com/contact">Contact Us</a> | <a href="http://wordpress.stackexchange.com/terms">Policies and Terms of Use</a> | <a target="_blank" href="http://www.boplinger.com">webmaster: boplinger.com</a></div>
          <!-- end #footer --> 
      </div>
    <!-- end #footer --> <?php 
      get_template_part('includes/scripts'); 
      wp_footer(); ?>
  </div> 
  <!-- end #sup-hold --> 
</div>
<!-- end #container -->

I do not get errors but it is difficult to test something like this without setting up a complete parallel environment. I notice that your template doesn’t use get_footer(). Why not?