WordPress.org

Forums

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

  1. geoffe
    Member
    Posted 9 years ago #

    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.

  2. klifix
    Member
    Posted 9 years ago #

    Well I did it and it now appears on the frontpage, only it says all (0) and not the number of replies. It doesn't count, or it might only see no replies. In fact there are many replies in the last posts so it must to have count something....?????

  3. geoffe
    Member
    Posted 9 years ago #

    I pasted my test code without modifying it. You want comment_post_ID ='{$post->ID}' in there, not comment_post_ID = '6', so it's:
    $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'");
    echo " ({$numcomments} replies)";

  4. klifix
    Member
    Posted 9 years ago #

    Yes! It works fine now! Thanx very much!

  5. carnold
    Member
    Posted 9 years ago #

    OK Geoffe, thank you so much for all your help and klifix too!! My site too now shows the comments; however, it shows them on the line. How do i get the replies to show next to the title? Here is the the line of code in question:
    echo "

  6. < a href=\"{$post->guid}\">"; echo "{$post->post_title}< /a>
  7. "; $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'");
    echo " ({$numcomments})";
    echo '
    ';
    }
    ?>

    <?php endif; ?>
    I have tried it here:
    echo "

  8. < a href=\"{$post->guid}\">"; echo "{$post->post_title}< /a>
  9. "$numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'");
    echo " ({$numcomments})";
    echo '
    ';
    }
    ?>

    <?php endif; ?>
    and here:
    echo "

  10. < a href=\"{$post->guid}\">"; echo "{$post->post_title}< /a>$numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'");
    echo " ({$numcomments});
  11. ";
    echo '
    ';
    }
    ?>

    <?php endif; ?>
    All these tries give me "parse error". I am still waiting on answer from Geoffe about donations?

  • geoffe
    Member
    Posted 9 years ago #

    You'd want to use the code in this order:

    $numcomments = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'");
    echo "<a href=\"{$post->guid}\">";
    echo "{$post->post_title} ({$numcomments})</a>"
    }
    ?>
    </ul>
    <?php endif; ?>

    And I can send you a link for paypal if you would make a donation; I very much appreciate it.

  • geoffe
    Member
    Posted 9 years ago #

    If you don't want the (#) inside the link, use this:
    echo "{$post->post_title}</a> ({$numcomments})"

    ...and for whatever reason, the problem with anchor (<a>) tags being parsed here is working properly, hence the removal of spaces.

  • carnold
    Member
    Posted 9 years ago #

    OK, sorry for my noviceness (i am slowly learning). Now it is working perfectly!! Thank you.
    I prolly need to send wordpress a donation too as i "found" this post and you on their site. Please send me a link to your paypal, either here or at carnold52000 at yahoo dot com.

  • geoffe
    Member
    Posted 9 years ago #

    Thanks, I've sent a link to you for my 'veryeasy' account.

  • world
    Member
    Posted 9 years ago #

    Sorry folks, I have to reopen this thread:

    My problem as an absolute PHP-newbie is, that I can't get anything posted here to work.

    I guess this is because i try to call the db from a (home)page outside of wp, which itself has been added to the site later and is installed in a subdirectory named 'blog'.

    I should be able to call the wpdb from outside of WordPress, right? My table-prefix is 'wpde01_'.

    I only want the last post being displayed with headline and excerpt and linked via the page-slug. Unfortunately the excerpt has to be shortened, too, so using RSS isn't an option.

    Any nice person around willing to support a newbie?

  • geoffe
    Member
    Posted 9 years ago #

    OK, you have two options. One, you write in
    <?php require_once('/blog/wp-config.php'); ?>
    at the top [and I mean very top] of your custom home page. If you do that, then you will be able to access the $wpdb object. Otherwise, $wpdb->get_var() wouldn't work, just as $wpdb->posts wouldn't work to reference the posts table. The code examples above should work with this change.

    Second option, you connect to the database using mysql_connect and the other PHP mysql functions.

  • world
    Member
    Posted 9 years ago #

    Thanks a lot, Geoffe.

  • philip3657
    Member
    Posted 8 years ago #

    I realize that this is sort of an old thread, but it is the closest thing that I have found to what I need help with.

    I'm trying to have a recent entry section on my page that has an ellisis so that the post titles do not go onto the next line.

    Here's what I have

    <div id="cats">
    <h2>Recent Entries</h2>

      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;
      }
      <?php query_posts('showposts=5'); ?>
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    • "> $shorten_title = truncate($post->post_title, 10);
      echo $shorten_title; ?>
      <span class="recent_date"><?php the_time('n.j') ?></span>
    • <?php endwhile; endif; ?>

    </div>

    I've been trying to follow some of the discussion in this thread and I have tried some of the code mentioned, but I've been getting lots of parsing errors and right now my page is just displaying a lot of code.

    You can see for yourself: http://www.dvdenthusiast.net

    Any help would be appreciated.

  • Kafkaesqui

    Posted 8 years ago #

    philip, PHP code must be wrapped in php tags: <?php ... ?>

    So:

    function truncate($string,$chars,$append = '...') {

    should start:

    <?php
    function truncate($string,$chars,$append = '...') {

    and:

    return $string;
    }

    should end:

    return $string;
    }
    ?>

    By the way, since you're doing nothing more than echoing $shorten_title, you can 'shorten' it even more to:

    <?php echo truncate($post->post_title, 10); ?>

  • philip3657
    Member
    Posted 8 years ago #

    Okay, I changed those things, but the recent entries is still acting up.

    Is there something wrong with the code I am using?

    Here's what it looks like right now:

    <div id="cats">
    <h2>Recent Entries</h2>

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

      <?php query_posts('showposts=5'); ?>
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      * "> <?php echo truncate($post->post_title, 10); ?> <span class="recent_date"><?php the_time('n.j') ?></span>

    <?php endwhile; endif; ?>

    </div>

    I was using this code before, which actually brought up my post titles, but was not truncating them.

    <div id="cats">
    <h2>Recent Entries</h2>
    <?php 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;
    }
    ?>

    </div>

    Is one of these sets of code better to use? How do I need to change them to make them actually link to posts and truncate?

  • philip3657
    Member
    Posted 8 years ago #

    Okay, I think I got it.

    The only thing that I don't really understand right now are the black dots that are showing up next to each link.

    Is that something in the code? The stylesheet?

  • Kafkaesqui

    Posted 8 years ago #

    "Okay, I think I got it."

    Yeah, it would be the value you're passing for the truncate() $char argument. In the second code block it's 60, which would only affect long titles.

    As for the black dots, if they're doing this on your setup, I wouldn't be able to tell from the...truncated code appearing above. So visit:

    http://wordpress.pastebin.ca

    Paste your code there, and reply back with the url you receive for it.

  • philip3657
    Member
    Posted 8 years ago #

    I'm guessing the problem is either in teh stylesheet or the sidebar, so I included both on here:

    http://wordpress.pastebin.ca/264127

    Thanks for your help by the way! I really appreciate it!

  • Kafkaesqui

    Posted 8 years ago #

    You're apparently just seeing standard list tag bullets. Try wrapping a <ul> element around it, which A. makes it valid XHTML, and B. should pick up the styling from your stylesheet the other unordered list tags in your sidebar already use:

    <h2>Recent Entries</h2>
    <ul>

    ...

    <?php endwhile; endif; ?>
    </ul>

  • 123

    Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags