Forums

[resolved] Mini loop links to recent posts and pages (4 posts)

  1. DeanK
    Member
    Posted 5 years ago #

    I'm using the Mini Loop code to include a small prompt to my recent blog posts at the top of my static home page (http://www.dmk.com.au). This returns the 3 latest posts.

    However, yesterday, I added a new *page* to my site, and now this is showing up in the "3 latest" mini loop of links (although if you click on it, it returns "Sorry, no posts matched your criteria" -- whereas when you click on one of the blog entries, it shows that post on a single page.

    The code is:

    <?php
      # Here starts Mini Loop trick: You can use this code, but _at your own risk_
      # If you want to improve this code, you're welcome ;)
      $how_many=3; //How many posts do you want to show
    ?>
    <ul class="front-post-title">
    <?
      $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
        WHERE post_status = \"publish\" ORDER BY ID DESC LIMIT " . $how_many);
      foreach($news as $np){
        printf ("<li><a href=\"index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);
      }
    ?>
    </ul>

    I changed my headline above the link to say that this is a link to posts/pages -- until I can fix it, as I only want it to return results for posts, and not show pages.

    I've looked through the Loop docs and support forum, but cannot find a suitable solution -- I guess that I need to tell it to ignore pages, but I don't know how to do that.

    Please help! Thanks in advance... Dean

  2. Chris_K
    Member
    Posted 5 years ago #

    As you noticed, Pages are in the Post table too. And they have the same status.

    However, Post_type column will help you.

    <?php
      # Here starts Mini Loop trick: You can use this code, but _at your own risk_
      # If you want to improve this code, you're welcome ;)
      $how_many=3; //How many posts do you want to show
    ?>
    <ul class="front-post-title">
    <?
      $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
        WHERE post_status = \"publish\" and post_type = \"post\" ORDER BY ID DESC LIMIT " . $how_many);
      foreach($news as $np){
        printf ("<li><a href=\"index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);
      }
    ?>
    </ul>
  3. DeanK
    Member
    Posted 5 years ago #

    HandySolo, that worked perfectly, thank you!

    Yes, I wasn't sure how to make it posts only -- your help is really appreciated!

    Another great reason to love WP open source... thanks muchly from Melbourne downunder!

  4. Chris_K
    Member
    Posted 5 years ago #

    Pleased to be of service. Happy blogging!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.