WordPress.org

Support

Support » Plugins and Hacks » [Resolved] The PHP code for the last 5 posts

[Resolved] The PHP code for the last 5 posts

  • Hello,

    Can someone please help me with this?

    This is the problem:

    On my frontpage (www.klifix.nl) I have insert to my sidebar the last 5 posts on the frontpage.
    I also want the last 5 posts of movies.klifix.nl & radio.klifix.nl on my frontpage to appear.
    The question is: How do have to edit the following php-code to do this?

    The code:

    <?php
    $today = current_time(‘mysql’, 1);

    if ( $recentposts = $wpdb->get_results(“SELECT ID, post_title FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_date_gmt < ‘$today’ ORDER BY post_date DESC LIMIT 15”)):
    ?>

      <li id=”recents”>
      <?php
      foreach ($recentposts as $post) {
      if ($post->post_title == ”)
      $post->post_title = sprintf(__(‘Post #%s’), $post->ID);
      echo ”

    • <font color=#000099><b>* </b></font>ID’>”;
      the_title();
      echo ‘
    • ‘;

      }
      ?>

    <?php endif; ?>

Viewing 15 replies - 31 through 45 (of 78 total)
  • Try removing the echo statements above and below
    $shorten_title = truncate($post->post_title, 30);
    echo $shorten_title;

    I can’t see exactly what code you have there; it might be causing the script to fail.

    As another test, try editing your query line for the main news table that does work well, with the table for your movies query. That is, just replace wp_2posts with the name of your movies or music tables, because that should be the only difference, right?.

    I tried and didn’t work (both things you mentioned), it’s strange, because the echo strings I use with the frontpage news as well, and that works fine…

    So the frontpage news that works well, you replaced the table name in that code, and only the table name, and it fails?

    Exactly!

    This doesn’t make much sense: the code you have working, for wp_2posts, is working fine. The code for movies_posts works well until you modify it.

    If you leave the code for movies_posts alone, and replace wp_2posts with movies_posts in the code that does work, it fails?

    That defies explanation. But another note is that you shouldn’t put the truncate function in more than once [the function truncate(... code]. You declare it once before it’s called and don’t repeat it again.

    Ok, obviously I did something wrong! I’ll go testing out things and get back to you when I succeed! Thanx for helping me out!

    You where right! I called the truncate function twice. I removed it once and that did the trick! Thanx!

    Look to see that the only thing you are changing is the table name (…FROM movies_posts… instead of …FROM wp_2posts…) between the code that works and the code that doesn’t. If the code works with one table name and not the other, with absolutely no other changes, something is wrong.

    LOL we where posting the same time! You solved the problem already, thanks anyway! 😉

    I’ve another question raised of this issue:

    How can I add the number of comments per post behind the last 5 posts like this:

    >> 13:45 TRAFFIC JAM IN AMSTERDAM (5 replies)

    ???

    It must be something simple, but I can’t seem to find what it is. Is there someone who knows this?

    Since you’re not using the loop, you can’t use the template tags, but you can make another query such as:
    if ( $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_approved = 1"))
    echo " ({$numcomments} replies)";

    I used this code, but it doesn’t show anything at all, there’s nothing changed.

    This is full code right now, without the nr of comments code in it:

    <?php
    $today = current_time(‘mysql’, 1);

    if ( $recentposts = $wpdb->get_results(“SELECT ID, post_title, post_date FROM wp_2posts WHERE post_status = ‘publish’ AND post_date_gmt < ‘$today’ ORDER BY post_date DESC LIMIT 25”)):
    ?>

      <li id=”recents”>
      <?php
      foreach ($recentposts as $post) {
      if ($post->post_title == ”)
      $post->post_title = sprintf(__(‘Post #%s’), $post->ID);

      $posttime = strtotime($post->post_date);
      $mytime = date(‘<font color=”red”>H:i</font>’, $posttime);
      $mydate = date(‘<font color=”red”>d/m</font>’, $posttime);
      $currtime = time();
      $today = date(‘d’,$currtime);
      $postday = date(‘d’,$posttime);
      if($postday==$today)
      echo $mytime;
      else
      echo $mydate;

      echo ” ID’>”;
      $shorten_title = truncate($post->post_title, 60);
      echo $shorten_title;
      echo ‘

      ‘;

      }
      ?>

    <?php endif; ?>

    So where has the commentscode go into to get it work?

    it would go after the echo $shorten_title; line

    and when you paste the code here, you should put a space in the link (anchor) tags so they are not converted like so: < a href="..."> and < /a> — there is a necessary space after the <

    and try:
    if ( $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = {$post->ID} AND comment_approved = 1")) echo " ({$numcomments} replies)";
    instead.

Viewing 15 replies - 31 through 45 (of 78 total)
  • The topic ‘[Resolved] The PHP code for the last 5 posts’ is closed to new replies.