• I may be in a bit over my head with this one. I’m building a site for a friend’s business and I’ve designed it so that they can easily update the site without needing to know a lot about WordPress, HTML, or CSS. As such, the front page is a “static” page with a Loop that calls blog posts with the category “Homepage” – in this sense, they have a long, 1-page site with each section being its own post. It’s working well, for the most part.

    There are a few sub-pages that will take you away from this main page. One of them is the blog. What I’d like to do is highlight the 3 most recent blog posts (which are categorized as “Blog”) on the homepage. I’ve read up on how to do this by adding another Loop, but my problem is that I need to add the code to a WordPress post, which doesn’t seem to like the PHP needed for the Loop. Does this make sense? I can’t figure out how to accomplish this. Any help is appreciated!

Viewing 11 replies - 1 through 11 (of 11 total)
  • but my problem is that I need to add the code to a WordPress post

    If you are using a custom page then you would just add the second loop to the page?

    Without seeing the theme code it is hard, I have different options for the twenty eleven theme, you would could adapt the code in the files from the child themes for your specific theme.

    Page of Posts this might give you the structure.

    How about a sidebat widget?
    Category Posts Widget you would need to add the theme widget page that creates the widget to your theme.

    HTH

    David

    Thread Starter dzumini

    (@dzumini)

    Thanks. Your comments caused me to dig further and it appears what I need to do is create a nested loop. The problem is, I’m having trouble figuring out exactly what the code should be…

    I want to show the 3 most recent posts from the category “Blog”. The pieces I have so far are:

    To pull the content from the category “Blog”

    // Get the last 3 posts in the Blog category.
    <?php query_posts('category_name=Blog&posts_per_page=3'); ?>
    
    <?php while (have_posts()) : the_post(); ?>
      <div id="posttitle">
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
      </div>
      <div id="blogmeta">
       Posted on <?php the_time('F jS, Y') ?>
      </div>
      <div class="blogentry">
       <?php the_content('Read the rest of this entry »'); ?>
      </div>
      <?php endwhile;?>

    For creating a nested loop:

    $my_query = new WP_Query( "cat=3" );
     if ( $my_query->have_posts() ) {
       while ( $my_query->have_posts() ) {
               $my_query->the_post();
               the_content();
        }
      }
    wp_reset_postdata();

    I’m guessing somehow these need to go together, but I can’t figure out how…

    Nope, the first snippet is all you need.

    It contains functions like the_content(), indicating the post object is there to use. Everything in your post can be displayed in the same area of the code.

    posts_per_page=3 limits it to 3

    Let me mention that your using the loop in the first snippet exactly as intended and commonly used.

    Using query_posts is generating the posts that the loop cycles through, you can customize the query_posts arguments a lot more.

    Thread Starter dzumini

    (@dzumini)

    Good to know – but somehow it’s still not working…

    The code in my post is exactly:

    <div id="frontpage">
    <?php query_posts('category_name=Blog&posts_per_page=3'); ?>
    
    <?php while (have_posts()) : the_post(); ?>
      <div id="posttitle">
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
      </div>
      <div id="blogmeta">
       Posted on <?php the_time('F jS, Y') ?>
      </div>
      <div class="blogentry">
       <?php the_content('Read the rest of this entry »'); ?>
      </div>
    <?php endwhile;?>
    </div>

    and that displays:

    “Posted on” with the text linked to “…/%3C?php%20the_permalink()%20?%3E%E2%80%9D%20rel=%E2%80%9Dbookmark%E2%80%9D%20title=%E2%80%9DPermanent%20Link%20to%20%3C?php%20the_title_attribute();%20?%3E%E2%80%9C%3E%3C?php%20the_title();%20?%3E%3C/a%3E%20%20%3C/div%3E%3Cdiv%20id=”

    Something’s missing? (And just FYI, I have at least 3 posts tagged “Blog”)

    Do you have a category named Blog and your posts are in there?

    Thread Starter dzumini

    (@dzumini)

    I do, there are currently 3 posts categorized as “Blog” (not tagged, as I accidentally stated above…)

    Oh sorry I just realized you said the code in your post.

    You need to put code in the template file that your visitor will be viewing. However I’m thinking you might need a short-code that goes in your post instead.

    Chances are it already exists. List 3 latest posts, very common request. Your original question goes into code, I assumed your working on the files. Look for plugins that list posts using short-codes.

    Thread Starter dzumini

    (@dzumini)

    Thanks very much! Your suggestion helped me find numerous shortcodes that will help me. Now I just need to customize them to display as I like, but for me that’s the easy part.

    For anyone else with a question like mine, the plugin I’m working with now is “Display Posts Shortcode” found here.

    Great glad I could help you make progress.

    Wish I could help you customize a short-code but I only have enough spare time for some quick help. Got customers for my Easy CSV Importer plugin waiting on a new version 🙂

    Thread Starter dzumini

    (@dzumini)

    Thanks, but no need – I’ve got it working exactly as I wanted! That said, it does have me interested in looking into what it takes to create shortcodes of my own. That’s another project though! One step at a time 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Highlighting Specific Posts Without the Loop?’ is closed to new replies.