• Resolved deank

    (@deank)


    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 (https://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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Chris_K

    (@handysolo)

    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>
    Thread Starter deank

    (@deank)

    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!

    Chris_K

    (@handysolo)

    Pleased to be of service. Happy blogging!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Mini loop links to recent posts and pages’ is closed to new replies.