• Hi there, I’m new to WordPress so forgive me if this question is obvious.

    I am currently working on a 6 page site using WordPress as the CMS. All 6 pages of the site (this does not include the home page) will include static information that I would like to keep updated once and a while. However, on three of the pages I would like to show posts from one specific category (for example on page 2 I would like to show static content as well as posts from category 5, page 3 — category 3, etc).

    So far in my page.php template I have set up the loop twice. Once to show the page content, and the second time I use an if statement and query post, then run the loop again – it looks like this:

    `<?php
    if (is_page(‘2’) ){
    query_posts(“category_name=test-category-one”);
    }
    ?>

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

    <h2><?php the_title(); ?></h2>
    <h4><?php the_time(‘F jS, Y’) ?>
    <?php /*?><!– by <?php the_author() ?> –> · <?php comments_popup_link(‘No Comments’, ‘1 Comment’, ‘% Comments’); ?><?php */?>
    </h4>
    <div class=”entry”>
    <?php the_content(‘<p>Read the rest of this page →</p>’); ?>
    <?php link_pages(‘<p>Pages: ‘, ‘</p>’, ‘number’); ?>
    </div>
    <?php if (‘open’ == $post-> comment_status) { ?>
    <p class=”tagged”>#comments”><?php comments_number(‘No Comments’, ‘1 Comment’, ‘% Comments’); ?></p>
    <div class=”clear”></div>
    <?php } else { ?>
    <div class=”clear rule”></div>
    <?php } ?>

    <?php endwhile; endif; ?>`

    The problem is this:
    1. I could use a few if statements to go through the pages / categories – but I was hoping there was a cleaner or more efficent way of doing this.

    2. For the pages that I don’t want posts to appear on, the loop just repeats and the page content appears twice.

    Does anyone have any suggestions – Just a reminder I’m new to WordPress and still a bit green with PHP.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi,

    Does this help?

    https://codex.www.ads-software.com/Creating_a_Static_Front_Page

    Towards the bottom, there are instructions for creating a static page with a mini loop.

    Thread Starter danaherlihey

    (@danaherlihey)

    hmmm not entirely sure.
    i’d still like to use the wordpress backend to edit the static pages (should I want to) and there is also the problem of not wanting to include posts on half the pages.

    You can edit the static pages in the Theme Editor. And the instructions on the link has code that shows you how to include what and how many posts you want on particular pages. (I think).

    danaherlihey, take a look at the Pages and The Loop in the Codex.

    For the pages where you want a loop of posts, you should use a page template with the specific query. The page.php file in your template should just be for a standard page.

    If you want a page with “static” content that can be edited normally inside WP, use two loops. The first loop should be for the pages static” content. For the second loop, use a custom query_posts query before the second loop.

    If you want the loop of posts first with the static content second, then the template for that page should have something like The Loop – Multiple Loops Example #2.

    When dealing with multiple loops, each loop should have its own query, otherwise the first query is used in the second query.

    Thread Starter danaherlihey

    (@danaherlihey)

    scribblerguy, does the rule that each loop should have its own query only apply when i have the loop of posts first and static content second, or is it a general rule to be applied whenever you have multiple loops?

    right now I have the standard page template but with a php if statement that is like this:

    <?php
         if (is_page('2') ){
    	  query_posts("category_name=test-category-one");
    	  include (TEMPLATEPATH . '/catdisplay.php');
           }
    	   ?>

    where catdisplay.php just has the standard loop written in. so far it seems to work fine. but maybe this isn’t the best way to go about it.

    does the rule that each loop should have its own query only apply when i have the loop of posts first and static content second, or is it a general rule to be applied whenever you have multiple loops?

    It’s not a rule per se. It’s just that in almost all uses of multiple loops, the reason for multiple loops is to generate different lists.*

    If you’re using a custom loop before the static content’s loop, then you’ll need to save the implicit query for that page (see the example I linked to earlier).

    As long as everything “works,” then you should be okay.

    *An example of when the same query could be ran for two loops is if the first loop was some sort of page contents listing (the title of the post and an anchor link (#title-of-post) to the second instance of the post, which had the full entry (the content).

    Take a look at how I pulled this off:

    https://screaminglight.com/projects/technical/wordpress-blog-posts-on-pages/

    Lots of pieces have to come together, but it’s completely doable. Shoot me an email if I was unclear, and good luck!

    cSoul <[email protected]> – remove the capital letters

    Check out Static Home Page with Dynamic Content article.
    Good luck.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘static page that also shows posts’ is closed to new replies.