• Resolved klifix

    (@klifix)


    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 - 46 through 60 (of 78 total)
  • Not trying to hijack this thread but i too am trying to do this on my site (www.mytimewithgod.net). When i insert the code given above, i get this error:

    WordPress database error: [Table ‘wpsite.wp_2posts’ doesn’t exist]
    SELECT ID, post_title, post_date FROM wp_2posts WHERE post_status = ‘publish’ AND post_date_gmt < ‘2006-06-10 01:43:42’ ORDER BY post_date DESC LIMIT 25

    So, it looks like my table name is different from “wpsite.wp_2posts”. I run this server and have full access to it. I use phpmyadmin; how do i find out what table name i need to use?
    **EDIT** My table for this code is:
    Browse: (327 Rows) wp_posts
    Now i get this error:
    20:30 ID’>
    Fatal error: Call to undefined function: truncate() in /srv/www/htdocs/wp-content/themes/[sometheme]/sidebar.php on line 104

    **EDIT again**
    Ok, i missed the earlier posts but now have gotten it to show up on my site. If you look, you will see they are not linked (clickable) and are not aligned in “list” format.
    @klifix-how did you “list” yours and make then linkable? Thanks for all the work!

    Instead of
    SELECT ID, post_title, post_date FROM wp_2posts ... you should be using
    SELECT ID, post_title, post_date FROM {$wpdb->posts} ...

    klifix was getting it to work because his table prefix is wp_2 but that wasn’t the right way to code it. I believe klifix was also perhaps crossing into wordpress tables that were for a separate site or section, a wp installation different from where the code was called.

    The lines that make the links are obscured above.

    Add in guid like so:
    SELECT ID, post_title, post_date, guid FROM ...

    Then you need to echo "< a href="{$post->guid}">";
    Before the truncated title and echo "< /a>"; after.

    As noted above, putting the space after the < is the one way I know to have these forums not convert the code into links and mess up the code.

    And carnold, you have some code for each post time that reads, for example: <fo630 cofridayofri="" 9="" jun="" 2006="" 20:30:30="" -0400="Fri, 9 Jun 2006 20:30:30 -0400e09">20:30</fo630> what is that?

    If you want the links in list format you should have <li> tags in there but you could do like kliffix and add <br /> after the < /a> tag.

    OK, i now have this for 1 line:
    if ( $recentposts = $wpdb->get_results(“SELECT ID, post_title, post_date guid FROM {$wpdb->posts} WHERE post_status = ‘publish’ AND post_date_gmt < ‘$today’ ORDER BY post_date DESC LIMIT 25”)):
    Right?

    Is this the trunacte line:
    $shorten_title = truncate($post->post_title, 60);
    If so, i have this:
    echo “< a href=\”{$post->guid}\”>”;$shorten_title = truncate($post->post_title, 60);”< /a>”; (of course minus the space before the < a)
    If you now look, thanks to your help, i have made some progress as it is linkable. But now the time is messed up and i still can’t get “last 5 posts”

    you look to be missing a comma after post_date

    instead of where it says LIMIT 25, you would want LIMIT 5 to get just the last 5

    see my edit in previous post about adding a <br /> tag or using <li> tags to separate the links into separate lines or a list. <li> would go before < a href=".... and then </li> should go after ‘< /a>`

    Do i need to remove some quotes from this:”< a href=\”{$post->guid}\”>”; “< /a>”;
    I have tried to remove the begining quotes and ending quotes like this: < a href=\”{$post->guid}\”>”; < /a>;
    This gives me a parse error. Also, when i put in
    after the echo “< a href=\”{$post->guid}\”>”;$shorten_title = truncate($post->post_title, 60);”< /a>”; i get a parse error:echo “< a href=\”{$post->guid}\”>”;$shorten_title = truncate($post->post_title, 60);”< /a>”;
    and this:
    echo “< a href=\”{$post->guid}\”>”;$shorten_title = truncate($post->post_title, 60);”< /a>”
    ;
    and this:
    echo “< a href=\”{$post->guid}\”>”;$shorten_title = truncate($post->post_title, 60);”< /a>
    “;
    Thanks again for your helping a novice!!

    When you are using ” in an echo statement, you need to escape any other ” you use or else you’ll confuse things.

    See here to find out the trouble of pasting code in this forum with escaped quotes. The escapes \ disappear after editing.

    You want echo "< a href=\"{$post->guid}\">";
    $shorten_title = truncate($post->post_title, 60);
    echo "$shorten_title < /a>";
    [But do you really need to truncate your titles? You could probably do without the truncate line and just echo “{$post->post_title} < /a>”;]

    I prolly dont need to truncate the titles. Thank you very much!! I think it looks better now that the escape characters have been taking care of. I am also trying to put the comments after the title using:
    if ( $numcomments = $wpdb->get_var(“SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = {$post->ID} AND comment_approved = 1″)) echo ” ({$numcomments} replies)”;
    in the echo $shorten_title; line but nothing shows up. Do you take donations for your help?

    If you don’t need to truncate titles, remove the code:
    $shorten_title = truncate($post->post_title, 60);
    echo "$shorten_title < /a>";

    and use:
    echo "{$post->post_title} < /a>";
    and after that line to do the comments:
    if ( $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = {$post->ID} AND comment_approved = 1")) echo " ({$numcomments} replies)";

    OK, i removed that truncate code. When using the comments code, it does not display. Here is the line of code:
    echo “

    • < a href=\”{$post->guid}\”>”; echo “{$post->post_title}< /a>
    • “; if ( $numcomments = $wpdb->get_var(“SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = {$post->ID} AND comment_approved = 1″)) echo ” ({$numcomments} replies)”;
      echo ‘
      ‘;
      Apparently, this is not right?

    Thread Starter klifix

    (@klifix)

    It doesn’t work for me as well. It all mess up the index file.

    might be because I was forgetting to enclose the vars in the SQL statement.
    How about with:
    if ( $numcomments = $wpdb->query("SELECT COUNT(*) FROM '{$wpdb->comments}' WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'")) echo " ({$numcomments} replies)";

    …and for some reason unbeknownst to me, get_var doesn’t work here, but query does. In my testing, the quote marks aren’t necessary, but changing get_var to query makes a difference.

    The code in my previous post should solve the trouble. The significant change is that $wpdb->get_var becomes $wpdb->query

    But a note for carnold: you should put in a </ul> tag between the end of the foreach statement and endif statement because it’s not being closed.

    So that:
    }
    ?>

    <?php endif; ?>
    becomes:
    }
    ?>
    </ul>
    <?php endif; ?>

    This will stop the indentation below Recent Posts.

    and it seems you should go back to get_var, I sort of had it right the first time, but use:
    $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '6' AND comment_approved = '1'");
    echo " ({$numcomments} replies)";

    …which removes the if () clause.

Viewing 15 replies - 46 through 60 (of 78 total)
  • The topic ‘The PHP code for the last 5 posts’ is closed to new replies.