WordPress.org

Ready to get started?Download WordPress

Forums

Blog posts on static front page (19 posts)

  1. kallywag
    Member
    Posted 6 years ago #

    I am displaying a static front page with the 'Front page displays' setting in the 'Reading' panel. This works great but I would like to pull in a list of recent blog posts (possibly by category) into this 'static' page, which is using its own page template. I'm not sure the best way to do this. I'm assuming it will require multiple 'Loops' since the page template already uses 'have_posts()' to pull in the 'static' content. I've done a bunch of searches for tips on how to do this, but a lot of the results seem outdated, so I'm looking for the most current method of achieving this with a fresh install of WordPress 2.5. Thanks for any help about how to achieve this!

  2. nathanrice
    Member
    Posted 6 years ago #

    here's the method I use to employ multiple loopps:

    <?php $my_query = "showposts=3&cat=5"; $my_query = new WP_Query($my_query); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <!-- Insert the regular template tags you would use within the loop here-->

    <?php endwhile; // end of one post ?>
    <?php endif; //end of loop ?>

    this method allows you to use multiple loops on your homepage without any of them inheriting settings from one another.

  3. kallywag
    Member
    Posted 6 years ago #

    That worked great. Just for anyone finding this post, what I have accomplished is pulling in blog post content into a 'static' page template so that I have static content and blog content on the same page. Here is the code I used with both loops, the first being the blog content loop and the second being the standard page template loop for static content:

    <?php $my_query = "showposts=3"; $my_query = new WP_Query($my_query); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    	<!-- standard tags to display blog post information like the_title() here -->
    
    <?php endwhile; // end of one post ?>
    <?php endif; //end of loop ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<!-- standard tags to display the static page information like the_title() here -->
    
    <?php endwhile; endif; ?>
  4. nathanrice
    Member
    Posted 6 years ago #

    yep, exactly. good going!

  5. kallywag
    Member
    Posted 6 years ago #

    Where would I find the function reference for the various parameters I can pass to WP_Query? Your example showed two possible parameters (showposts & cat) but are there others? Where are they listed? Is there one that can specify a 'static' page so, for instance, you could pull in 'static' wordpress page content into a blog post page or the main blog page? I looked at the WP_Query codex page and it didn't seem to list them, but I'm guessing these parameters are not specific to WP_Query.

  6. sebi69
    Member
    Posted 6 years ago #

    Oh boys. That's exactly what I've been praying for.

  7. jwesseldyke
    Member
    Posted 6 years ago #

    Doing this in 2.3 was easier, wasn't it? Anyway, this 2.5 thing is confusing to me...

    This may be a dumb question, but I played with the snippet above and it kinda worked... Placed the above in a template, created a page that used the template instead of default...

    I'm trying this with a couple categories and one post in each category at a time until I figure out what I'm doing, so...

    The result was an article title (which linked appropriately to the post) from the appropriate category, author and date... Then from another category, a title, author and date and the entire post.

    So, I have a question...

    How do I change the above snippet, and where do I put it in the template file (I'm not a coder, so "the loop" is beyond my comprehension... I'm looking for "after xyz paste the snippet, add these specific tags, then paste the endif's") to get only post titles (that link to posts) and the category the article is posted to.

    Side note: I have about 10 categories and I want to display the most recent (only one) post title from each category.

    I'm going cross-eyed and looking for the Tylenol over this one...
    Thanks for any help...
    J

  8. abelq
    Member
    Posted 6 years ago #

    nathanrice/kallywag, this is absolutely wonderful! Worked like a charm the first time I copy and pasted it. Thank you.

  9. abelq
    Member
    Posted 6 years ago #

    Furthermore, while displaying blog posts on a static page, I removed posts from a category by adding &cat=-8 on this line

    <?php $my_query = "showposts=6&cat=-8"; $my_query = new WP_Query($my_query); ?>

  10. jerrichou
    Member
    Posted 6 years ago #

    Hey there. I tried this code but tried to use it to pull in posts in their original form (with excerpts etc.)

    Instead of showing the querryed posts, it shows all posts. I know the reason is probably b/c the post its querying aren't the ones the php is calling for the permalink but was wondering if anyone knew how to fix this?

    Here's the code: Thanks soooo much!

    <!--post loop starts here-->

    <?php $my_query = "showposts3&cat110"; $my_query = new WP_Query($my_query); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>

    <div class="postthumb">"><?php post_image('', true); ?><?php
    $imagethumb = get_post_meta($post->ID, "imagethumb", true);
    echo $imagethumb;
    ?>
    </div>

    <h2 style="margin-left:204px">" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></h2>
    <h4 style="margin-left:204px">
    <?php the_time('F jS, Y') ?> by <?php the_author() ?><!-- · <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>--></h4>
    <div class="entry">
    <p style="margin-left:204px">
    <?php the_excerpt () ?>

    #more-<?php the_ID(); ?>" title="Read the rest of this entry">[Read more →]</p>
    </div>
    <p class="tagged">

    <span class="add_comment"><?php comments_popup_link('→ No Comments', '→ 1 Comment', '→ % Comments'); ?></span>
    Tags:
    <?php the_category(' · ') ?>

    </p>
    <div class="clear"></div>
    <?php endwhile; // end of one post ?>
    <?php endif; //end of loop ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <!-- standard tags to display the static page information like the_title() here -->

    <?php endwhile; endif; ?>

  11. Follow the rabbit
    Member
    Posted 6 years ago #

    A million thanks for this! :D

  12. Clicknathan
    Member
    Posted 6 years ago #

    Does anyone know if there would be a conflict displaying # of comments in this scenario?

  13. dannysimple
    Member
    Posted 5 years ago #

    i cant get this to work when i put it into my home page post. my url is http://dannysimple.com if that helps. do i put it in some theme code (wouldn't that fuck up my site?) please reply to me though at the contact page in my website because i don't check this site much

  14. virtuallynicky
    Member
    Posted 5 years ago #

    Hi

    I want to do something very similar to this - but have the static content (intro to the website) above the most recent blog entries (excerpts of the last 5, to be exact). I can see it would be a version of this, how could I adapt it so it displays in this way?

    Thank you in advance

  15. llamaman
    Member
    Posted 5 years ago #

    @virtuallynicky

    I am expanding on @kallywag's code to hopefully make it a bit more clear. Note that "p=48" - that "48" is the id of a static blog post of your choosing.

    <?php $my_query = "p=48"; $my_query = new WP_Query($my_query); ?>
    <?php if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    	<div class="post" id="post-<?php the_ID(); ?>" style="margin-top:0px; padding-top:0px;">
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                        <?php the_title(); ?>
                        </a></h2>
                    <small>
                    <?php the_time('F jS, Y') ?>
                    <!-- by <?php the_author() ?> -->
                    </small>
                    <div class="entry">
                        <?php the_content('Read the rest of this entry &raquo;'); ?>
                    </div>
                    <p class="postmetadata">
                        <?php edit_post_link('Edit', '', ' | '); ?>
    
                </div>
                <!--/entry-->
            </div>
            <!--post-->
    
    <?php endwhile; // end of one post ?>
    <?php endif; //end of loop ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	 <div class="post" id="post-<?php the_ID(); ?>" style="margin-top:0px; padding-top:0px;">
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                        <?php the_title(); ?>
                        </a></h2>
                    <small>
                    <?php the_time('F jS, Y') ?>
                    <!-- by <?php the_author() ?> -->
                    </small>
                    <div class="entry">
                        <?php the_content('Read the rest of this entry &raquo;'); ?>
                    </div>
                    <p class="postmetadata">
                        <?php edit_post_link('Edit', '', ' | '); ?>
    
                </div>
                <!--/entry-->
            </div>
            <!--post-->
    
    <?php endwhile; endif; ?>
  16. virtuallynicky
    Member
    Posted 5 years ago #

    Thanks llamaman, I'll try that out :)

  17. drumz
    Member
    Posted 5 years ago #

    This is exactly what I was looking for but I need to put it on a front/home page. How do I link to the blog when the page is in the parent directory? In other words, where does the blog/ go?
    Thanks

  18. bitkahuna
    Member
    Posted 5 years ago #

    drumz, not quite following. the blog by default DOES go on the front/home page. you can have a 'static' or other custom home page by creating a page and then setting that as the home page in the 'reading' settings section. or you can modify the template (index.php typically for home, or page.php for other pages, or create your own) for the home or other page to do what you want.

  19. bitkahuna
    Member
    Posted 5 years ago #

    really appreciate this info but i can't display info about comments in this custom loop, because comments_popup_link apparently only works in 'the' loop.

Topic Closed

This topic has been closed to new replies.

About this Topic