Forums

conditional tags, query posts, and page template (12 posts)

  1. matttail
    Member
    Posted 3 years ago #

    I am planning to use conditional tags and query posts to have pages that display certain categories from my blog. I have accomplised that, but currently the text from the page it self is no longer displaying on the page.

    For example, I created a page called News. In the admin section when writing the new page I typed that this page would be used to show current news articles. In the page template I use conditional statements to say that if this is page 14 (the news page) then display entries from the news category I created (cat 6). This works, but the text that I typed in for the page no longer displays. I assume this is the fault of my conditional query post, as pages that aren't affected by the if statement still show the page content. What do I need to do to fix this?

    Here's my page code. thanks for any help or suggestions!

    <?php get_header(); ?>
    <?php include(TEMPLATEPATH."/sidebar1.php");?>
    <div id="main">
    	<div id="content">
    <?php
    if (is_page('6')) {
    query_posts('cat=3');
    }
    ?>
    <?php
    if (is_page('14')) {
    query_posts('cat=6');
    }
    ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><?php the_title(); ?></h2>
    			<div class="entrytext">
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    				<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    
    			</div>
    		</div>
    	  <?php endwhile; endif; ?>
    
    	<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    	</div>
    </div>
    
    <?php include(TEMPLATEPATH."/sidebar2.php");?>
    
    <?php get_footer(); ?>
  2. Roger Theriault
    Member
    Posted 3 years ago #

    That makes a lot of sense since the code you wrote is issuing a new query and ignoring/tossing the one set up already by WordPress.

    If you're trying to show a summary of related posts like that, don't REPLACE your loop, add your conditionals and a new loop AFTER the built-in loop (from page.php).

    http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_1

  3. matttail
    Member
    Posted 3 years ago #

    Excellent, I understand now and have it showing the page text and calling my conditional info. Thanks! But now I have an other problem. On pages that aren't referenced by my conditional tags it's showing an error. Here's my code:

    <?php
    	if (is_page('6')) { $my_query = new WP_Query('cat=3'); }
    	if (is_page('14')) { $my_query = new WP_Query('cat=6'); }
    	while ($my_query->have_posts()) : $my_query->the_post(); ?>

    The problem is that for pages that are not 6 or 14 my_query is not defined. But how do I change this so it doesn't produce error on those pages?

  4. Chris_K
    Member
    Posted 3 years ago #

    I'm probably missing the point, but why not use Category_Templates?

  5. matttail
    Member
    Posted 3 years ago #

    That doesn't seem to accomplish what I'm trying to do, if I read right. I'm using WP as a CMS for a website for a client. On the main page I am showing a welcome entry and the two most recent news articles. Then I have certain pages that I've created where I want to show posts from specific categories and other pages can just show what ever is written on that page. This way I can have certain pages that do what I want, and the client can later add on extra content on their own.

    So far I'm really digging WP, it's admin section is very easy to use, and the site is not that big which is why I want a nice light CMS like this.

  6. Roger Theriault
    Member
    Posted 3 years ago #

    You just need to precede your error-causing method call (since the object $my_query doesn't exist) with a simpler check.

    while ($my_query && $my_query->have_posts()) : $my_query->the_post();

    If $my_query is undefined, the result is logically false, and PHP doesn't need to execute the statements after the && (because that wouldn't change the result).

  7. matttail
    Member
    Posted 3 years ago #

    You're the best. Thanks so much for your help - in both of my threads. It's working perfectly now.

  8. Justin Tadlock
    Member
    Posted 3 years ago #

    I still don't see the reason for not just using a category template. Pulling posts from specific categories onto a page makes WP no more a CMS than what it was before.

    Basically, what you've done is just created a category template as a page template with a lot more work and a lot less functionality.

    I'd take HandySolo's advice and look into category templates.

    Also, the function link_pages() is deprecated. Use wp_link_pages() instead:
    http://codex.wordpress.org/Template_Tags/wp_link_pages

  9. Roger Theriault
    Member
    Posted 3 years ago #

    Actually the problem I see with a category template is inability to easily edit the category-level text, which it seems is needed in this case. With a page serving as a blog category main page, but still serving up its own content, you have a bit more flexibility.

    What I'd suggest, however, is totally different... Taking matttail's example above, suppose you wanted a "jumping off" page for some news content within your site, and you wanted to make it look a lot different than just a category archive. I'd recommend instead of adding conditionals to the page.php, that you use a custom page template (http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates) and apply that template to the desired page. This kind of separates the "hardcoding" of the page id and category id a bit, but allows greater customization of the page layout and backend (ie post pulling) logic.

    There are samples in the Codex but without concrete real world examples it's hard to visualize. So here goes...

    Let's say you wanted a Wall Street Journal "page one style" two-column news snippet section plus illustrated versions of your top two stories; on the Sports page, however, you want a couple of extra box score widgets and a video of the week.

    Now, you can sort of do that with a category template... but you're tied to one category... after all isn't that your category archive? Supposing you have writers contributing dozens of stories a day, and you want your main sport to switch from basketball to baseball to football as the seasons change, yet highlight whatever playoffs you need to highlight? A custom Sports Front Page template applied to a page can have multiple Loops to handle all this. Even logic to promote stories that are getting lots of attention (possibly taking hints from custom fields inside that page... or extra database fields tacked on by a plugin... so the "Edit Page" on the admin side gives you a bit of control). That's more user friendly, to me, and more like the big CMSes that cost big bucks.

  10. Justin Tadlock
    Member
    Posted 3 years ago #

    "Category-level text" can easily be edited from the WP dashboard and shown on the page using category templates and not using pages.

    Now, if you need more dynamic content or to serve up multiple categories, then a page template is the way to go.

    Looking at the original post on this thread though, I see no reason to not use a category template.

  11. matttail
    Member
    Posted 3 years ago #

    Thanks for all the suggestions. For the site in question I have a relatively simple 3 column layout. On the left, the pages I have are automatically listed on the left, content (posts) in the center, and some static contact content on the right.

    I started working with the pages, and setting up the page template to call the specific entires I want because the theme I started working from did that. This is also the first real project I've done with wordpress so I pretty much jumped in head first not knowing how to do what I wanted inside wordpress.

    I like the pages because it's very easy to let the end user edit the content them selves. The end user can go into wp-admin, easily add new posts to a specific category, pages that will automatically be linked, and or links to show on the side of the main page. They can just as easily jump in and edit content that's already there. For this project in particular it's very important that things be both obvious and easy to accomplish. While you may be able to edit category level text from the backed, it's not as obviously placed as the pages are.

    I have the main page, which uses the template index.php do a wp query call a specific post - my welcome post. After that I have a new wp query call the two most recent posts from the news category. Next I have several pages that all reference page.php as the template. Most of those pages just hold a bit of information that's entered directly on that page when creating it. A few of those pages, two right now, meet one of the conditional if statements I set up to show posts from a specific category. That my "news" page and a "blog" page.

    The way I'm working it the pages are less of jumping off places and more of the ending. It also seems like it will work out well for contributors who have trouble using programs like MS word. You're welcome to look at the site if it makes any difference. It's still a little un-finished and broken in some places right now.
    http://stjohnsbreese.org - a church website.

  12. gmisen
    Member
    Posted 3 years ago #

    hey what did your code look like once you got this to work?

    i'm trying pretty much the same thing (i think) but my new query keeps breaking the page.

    i have this:

    <?php   while ($my_query && $my_query->have_posts()) : $my_query->the_post();
    
    if (is_page('445')) { $my_query = new WP_Query('category_name=team&showposts'); }
    
    if (is_page('460')) { $my_query = new WP_Query('category_name=localexperts&showposts'); }
    
    while ($my_query->have_posts()) : $my_query->the_post(); ?>

    i can't figure out why it's not working...any suggestions?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.