• 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 - 61 through 75 (of 78 total)
  • Thread Starter klifix

    (@klifix)

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

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

    Thread Starter klifix

    (@klifix)

    Yes! It works fine now! Thanx very much!

    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 ”

    • < 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})”;
      echo ‘
      ‘;
      }
      ?>

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

    • < 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})”;
      echo ‘
      ‘;
      }
      ?>

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

    • < 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});
    • “;
      echo ‘
      ‘;
      }
      ?>

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

    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.

    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.

    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.

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

    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?

    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.

    Thanks a lot, Geoffe.

    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: https://www.dvdenthusiast.net

    Any help would be appreciated.

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

    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?

    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?

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