WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] The PHP code for the last 5 posts (79 posts)

  1. klifix
    Member
    Posted 8 years ago #

    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; ?>

  2. geoffe
    Member
    Posted 8 years ago #

    So you have three WordPress installations on the same server?

    The $wpdb object will reference only the database of the WP installation at http://www.klifix.nl, which I presume is running on the same database as movies.klifix.nl but has a different table prefix.

    If this is the case, all you need to fix is the $wpdb->posts in your SQL query to the name of the table for posts at movies.klifix.nl

    Perhaps it is wp_movies_posts, so then you would want
    "SELECT ID, post_title FROM wp_movies_posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 15"

  3. klifix
    Member
    Posted 8 years ago #

    and if it's on a different server?

  4. davidchait
    Member
    Posted 8 years ago #

    Easiest is to use an rss aggregator (like my CG-FeedRead plugin), and use it to grab and list the last N entries in each site's RSS feed. that's fast and cached, and doesn't require writing SQL queries yourself. ;)

  5. klifix
    Member
    Posted 8 years ago #

    I succeed! Thanx very much!!!

  6. geoffe
    Member
    Posted 8 years ago #

    yes, you'd likely find the best ease with an rss aggregator.

    But how (or why) would you have your subdomains on a different web server?

    While I don't know anything of mr. chait's plugin, I have found this plugin to be very handy. You'll probably want to test a couple (there are many out there) to see what works for you.

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    And then there's always the codex: http://codex.wordpress.org/Function_Reference/fetch_rss

  8. klifix
    Member
    Posted 8 years ago #

    I have a few different websites. From some websites i get a few or more databases. I use a few of 6 databases for my website. That's why, but for Klifix I just use 2 databases, and it works fine!

  9. klifix
    Member
    Posted 8 years ago #

    By the way: Thanx very much for the good help!!!

  10. geoffe
    Member
    Posted 8 years ago #

    so it is different databases, not different servers?

    That would mean that you could just query the other database fairly easy, but using rss might be simpler for you, if not as direct a method.

  11. klifix
    Member
    Posted 8 years ago #

    Maybe it is! I will check it out!

    I have a second question about this. Next to the last 5 posts i want to have de date & time of the posts. How can i do that in the last 5 posts?

  12. geoffe
    Member
    Posted 8 years ago #

    well, you're calling the posts from your other WP installation through an RSS feed?

    All the data you can have is in your feed.

    So if you were using fetch_rss, much like how you call the link to the post by $item['link'], you would call your date on the post by $item['pubDate']. Then you could format that date using strtotime and the date functions.

    So like this:
    $thetime = strtotime($item['pubDate']);
    $thetime = date('Y-m-d H:i', $thetime );
    echo $thetime;

    See the format characters on the date page for other options in formatting.

  13. klifix
    Member
    Posted 8 years ago #

    Thanks for your respons!

    I'm not calling from rss feed, instead I use the above mentioned php code. Where do I have to put the timecode in this code?

  14. geoffe
    Member
    Posted 8 years ago #

    sorry, I don't know what PHP code you are referring to.

    are you making a query to the database of your other site to get posts?

  15. klifix
    Member
    Posted 8 years ago #

    Sorry, this one:

    <?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; ?>

  16. geoffe
    Member
    Posted 8 years ago #

    Ok, just change
    $wpdb->get_results("SELECT ID, post_title FROM
    to
    $wpdb->get_results("SELECT ID, post_title, post_date FROM

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

  17. klifix
    Member
    Posted 8 years ago #

    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.

  18. geoffe
    Member
    Posted 8 years ago #

    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'>"

  19. che1959
    Member
    Posted 8 years ago #

    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.

  20. klifix
    Member
    Posted 8 years ago #

    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?

  21. klifix
    Member
    Posted 8 years ago #

    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/

  22. geoffe
    Member
    Posted 8 years ago #

    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.

  23. klifix
    Member
    Posted 8 years ago #

    thanx very much!

  24. klifix
    Member
    Posted 8 years ago #

    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?

  25. geoffe
    Member
    Posted 8 years ago #

    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.

  26. klifix
    Member
    Posted 8 years ago #

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

  27. klifix
    Member
    Posted 8 years ago #

    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; ?>

  28. geoffe
    Member
    Posted 8 years ago #

    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;

  29. klifix2
    Member
    Posted 8 years ago #

    ok thanx! You're very kind!

  30. klifix2
    Member
    Posted 8 years ago #

    it works perfect! Superb thanks very much!!!

Topic Closed

This topic has been closed to new replies.

About this Topic