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 - 16 through 30 (of 78 total)
  • Where do I have to past? this code?

    and then in your loop call it using $post->post_date when you want to print it.

    anywhere after the foreach and before this:
    }
    ?>

    <?php endif; ?>

    You can put in a line such as:
    echo $post->post_date;
    to print the date and time of the post, such as after the_title();

    Although I’m not sure where you got this:
    echo “
    <font color=#000099><b>* </b></font>ID’>”;

    What that does is print:
    * ID’>”

    I am using the feedlist.php plugin. It is very easy and customizable. I can’t really understand all of the code posted on this thread, but feedlist does the job. Can’t remember the link to it, but just do a search for feedlist.php wordpress plugin and you will get it.

    ok thanx! It works fine. Only the whole date appears, I just want to let the time of posting to appear, is that possible as well?

    What I actually need for my website is the same as the Dutch website Fok does (see below)

    on the left sidebar you can see what I mean:

    http://frontpage.fok.nl/

    Instead of echo $post->post_date;

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

    There’s likely a more elegant way to do the above but it gets the effect that FOK has, which is to print the time of the post if it’s from today, and the date of the post if it’s earlier than today.

    thanx very much!

    It works perfect. Just one last question. I’d like to outline the posts, but if the post sentence is to long I’d like to break the postname, like:

    12:30 Breakers wins de finale Dutch open
    2006 in Rotterdam

    must be:

    12:30 Breakers wins the finale open…

    Is there a way to do this?

    Sure try this:

    Add this function in anywhere before you call it:

    function truncate($string,$chars,$append = '...') {
    if(strlen($string) > $chars) {
    $string = substr($string, 0, $chars);
    $trunc_at = strrpos($string, ' ');

    if($trunc_at !== FALSE)
    $string = substr($string, 0, $trunc_at);
    $string = $string.$append;
    }

    return $string;
    }

    And then we use this like so:
    instead of calling the_title(), you’ll need to mess with $post->post_title and then echo the modified form of $post->post_title where you would have used the_title() to print the title.

    $shorten_title = truncate($post->post_title, 60);
    echo $shorten_title;

    This searches for a space somewhere before the 60th character of the string and truncates the title to that spot and adds ‘…’ to it.

    Superb, thanx very much, I’ll go trying!

    Well, I tried many times, but I’m not really an PHP expert so it didn’t work. I don’t really exactly know where to put the diverse codes into and change things.

    This is the code I know have, how do I have to modifie it to get the (…) function?

    <?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 10”)):
    ?>

      <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’>”;
      the_title();
      echo ‘

      ‘;

      }
      ?>

    <?php endif; ?>

    After the line : $today = current_time('mysql', 1);
    put in the truncate function. That whole code block. This function could go in many places, but the important thing is that it is declared before it is called.

    Then the line you have with the_title();
    you replace that line with:
    $shorten_title = truncate($post->post_title, 60);
    echo $shorten_title;

    ok thanx! You’re very kind!

    it works perfect! Superb thanks very much!!!

    Thanks for helping, it works fine with the frontpage-news although, but when I try it with the php code on the frontpage to get the news of the movies and music subdomains the whole page gets blank. This is the code I use on the frontpage to get the news (posts) from the movies.klifix.nl and music.klifix.nl:


    <b>Laatste Filmnieuws:</b></font>
    <?php
    $today = current_time('mysql', 1);

    if ( $recentposts = $wpdb->get_results("SELECT ID, post_title, post_date FROM movies_posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 5")):
    ?>

      <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'>";
      the_title();
      echo '

      ';

      }
      ?>

    <?php endif; ?>

    and this is how I modified it:


    <b>Laatste Filmnieuws:</b></font>
    <?php
    $today = current_time('mysql', 1);


    function truncate($string,$chars,$append = '...') {
    if(strlen($string) > $chars) {
    $string = substr($string, 0, $chars);
    $trunc_at = strrpos($string, ' ');

    if($trunc_at !== FALSE)
    $string = substr($string, 0, $trunc_at);
    $string = $string.$append;
    }

    return $string;
    }

    if ( $recentposts = $wpdb->get_results("SELECT ID, post_title, post_date FROM movies_posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 5")):
    ?>

      <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, 30);
      echo $shorten_title;

      echo '

      ';

      }
      ?>

    <?php endif; ?>

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