WordPress.org

Forums

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

  1. klifix
    Member
    Posted 9 years ago #

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

  2. geoffe
    Member
    Posted 9 years ago #

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

  3. klifix
    Member
    Posted 9 years ago #

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

  4. geoffe
    Member
    Posted 9 years ago #

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

  5. klifix
    Member
    Posted 9 years ago #

    Exactly!

  6. geoffe
    Member
    Posted 9 years ago #

    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.

  7. klifix
    Member
    Posted 9 years ago #

    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!

  8. klifix
    Member
    Posted 9 years ago #

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

  9. geoffe
    Member
    Posted 9 years ago #

    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.

  10. klifix
    Member
    Posted 9 years ago #

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

  11. klifix
    Member
    Posted 9 years ago #

    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)

    ???

  12. klifix2
    Member
    Posted 9 years ago #

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

  13. geoffe
    Member
    Posted 9 years ago #

    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)";

  14. klifix
    Member
    Posted 9 years ago #

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

  15. klifix
    Member
    Posted 9 years ago #

    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?

  16. geoffe
    Member
    Posted 9 years ago #

    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.

  17. carnold
    Member
    Posted 9 years ago #

    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

  18. carnold
    Member
    Posted 9 years ago #

    **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!

  19. geoffe
    Member
    Posted 9 years ago #

    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.

  20. geoffe
    Member
    Posted 9 years ago #

    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.

  21. carnold
    Member
    Posted 9 years ago #

    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"

  22. geoffe
    Member
    Posted 9 years ago #

    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>`

  23. carnold
    Member
    Posted 9 years ago #

    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!!

  24. geoffe
    Member
    Posted 9 years ago #

    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>";]

  25. carnold
    Member
    Posted 9 years ago #

    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?

  26. geoffe
    Member
    Posted 9 years ago #

    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)";

  27. carnold
    Member
    Posted 9 years ago #

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

  28. < a href=\"{$post->guid}\">"; echo "{$post->post_title}< /a>
  29. "; 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?

  • klifix
    Member
    Posted 9 years ago #

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

  • geoffe
    Member
    Posted 9 years ago #

    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.

  • geoffe
    Member
    Posted 9 years ago #

    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.

  • 123

    Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags