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 - 1 through 15 (of 78 total)
  • 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”

    and if it’s on a different server?

    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. 😉

    I succeed! Thanx very much!!!

    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.

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

    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!

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

    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.

    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?

    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.

    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?

    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?

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

    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.

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