-2

I tried this code, but it totally broke my site. I suppose it's because I'm doing something wrong in the WP_Query's sacred posts context in the sidebar template. Could some WP guru please correct this code?

<?php 

$args = array(
    'orderby' => 'date'
    ,'order' => 'DESC'
    ,'showposts'=>6
);

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {

  echo "<dl>";

  while ($my_query->have_posts()) : $my_query->the_post(); 

  $recent_title = get_the_title();
  $recent_link  = get_the_permalink();
  $recent_date  = get_the_date();
  $recent_count = $post->comment_count;
  if ($recent_count < 1)        { $recent_count = 'Leave a Comment&nbsp;'; }
  else                          { $recent_count = "$recent_count <span class='iv'>Comments&nbsp;</span>"; }


  echo "<dt>$recent_date &nbsp; / &nbsp; <span class='comment'><a href='$recent_link#disqus_thread'>$recent_count</a></span></dt><dd><a href='$recent_link' title='$recent_title'>$recent_title</a></dd>"

  echo "</dl>";

  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().

?>

Just in case this doesn't appear properly formatted, here's the code on Gist

I really do NOT want to use a plugin for this simple functionality.

  • This is not WP_Query's sacred posts context, but plain pure php. The same syntax for php goes for Wordpress. Get yourself a proper syntax highlighter, this will help you in future – Pieter Goosen Aug 3 '14 at 13:54
  • Add the missing semicolon. – fuxia Aug 3 '14 at 15:29
  • I'm not sure this is off topic. If the semicolon was the only missing piece, which makes it a non-Wordpress issue, then sure. Please delete this thread. The community here is awesome, I got the help I needed. (And I already use Coda2, sometimes color coding is not the only thing needed.) Thanks. Btw, further down in the thread there is indeed a helpful variation to the code in the OP. – PKHunter Aug 4 '14 at 5:57
0

';' is missing in this line echo "<dt>$recent_date &nbsp; / &nbsp; <span class='comment'><a href='$recent_link#disqus_thread'>$recent_count</a></span></dt><dd><a href='$recent_link' title='$recent_title'>$recent_title</a></dd>"

For this reason code is throwing the fatal error. Please replace that line with this code

echo "<dt>$recent_date &nbsp; / &nbsp; <span class='comment'><a href='$recent_link#disqus_thread'>$recent_count</a></span></dt><dd><a href='$recent_link' title='$recent_title'>$recent_title</a></dd>";
  • Thanks. That was it! :) Also, what could I add in my code to make sure the current post is excluded from this listing of other recent posts? Thanks! – PKHunter Aug 3 '14 at 13:49
  • Please indent your code properly – Pieter Goosen Aug 3 '14 at 13:55
0

I did not test the code. But you can try this code.

$args = array(
    'orderby' => 'date'
    ,'order' => 'DESC'
    ,'showposts'=>6
);

if( is_singular('post')){
  global $post;
  $args['post__not_in'] = array( $post->ID); // Excluding the current post at post details page
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.