treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] WordPress Conditional Statement - Mini Loop Help!

  • Hi,

    I posted a question in the PHP help section a couple days back without any replies. Thought maybe since it was wordpress / php geared that it might fit in here better.

    I am having an issue with a conditional statement on my page.php file that checks for page name and if page name "A" then show a mini loop of posts from category 1, elseif page name B then show from cat 2 etc.

    Can anyone have a quick peek at my coding attempt and help me find my error(s) - or perhaps suggest a better way of handling this?

        <?php get_header(); ?>
    <div id=\"content\">
    <div id=\"main\" class=\"narrowcolumn\">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class=\"post\" id=\"post-<?php the_ID(); ?>\">
    <h2><?php the_title(); ?></h2>
    <div class=\"entry\">
    <?php the_content('<p class=\"serif\">Read the rest of this page &raquo;</p>'); ?>
    </div>
    </div>
    <?php endwhile; endif; ?>
    <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    </div>
    <?php if ( is_page('About') ) { ?>
    <h3>Read The Latest News</h3>
    <?php
    $recentnews = new WP_Query();
    $recentnews->query('category_name=News&showposts=3');
    ?>
    <?php while ($recentnews->have_posts())
    : $recentnews->the_post();
    ?>
    <div id=excerpt-mini-loop>
    <h3><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <p><a href=\"<?php the_permalink(); ?>\">Read the rest of this story...</a></p>
    </div>
    <?php endwhile; ?>
    <?php } elseif ( is_page('Lessons') ) { ?>
    <h3>Read Articles about Lessons</h3>
    <?php
    $recentlessons = new WP_Query();
    $recentlessons->query('category_name=Lessons&showposts=3');
    ?>
    <?php while ($recentlessons->have_posts())
    : $recentlessons->the_post();
    ?>
    <div id=excerpt-mini-loop>
    <h3><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <p><a href=\"<?php the_permalink(); ?>\">Read the rest of this story...</a></p>
    </div>
    <?php endwhile; ?>
    <?php } elseif ( is_page('Training') ) { ?>
    <h3>Read Training Articles</h3>
    <?php
    $recentarticles = new WP_Query();
    $recentarticles->query('category_name=training-articles&showposts=3');
    ?>
    <?php while ($recentarticles->have_posts())
    : $recentarticles->the_post();
    ?>
    <div id=excerpt-mini-loop>
    <h3><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <p><a href=\"<?php the_permalink(); ?>\">Read the rest of this story...</a></p>
    </div>
    <?php endwhile; ?>
    <?php } elseif ( is_page('Showing') ) { ?>
    <h3>Read Articles on Showing</h3>
    <?php
    $recentshowing = new WP_Query();
    $recentshowing->query('category_name=showing-articles&showposts=3');
    ?>
    <?php while ($recentshowing->have_posts())
    : $recentshowing->the_post();
    ?>
    <div id=excerpt-mini-loop>
    <h3><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <p><a href=\"<?php the_permalink(); ?>\">Read the rest of this story...</a></p>
    </div>
    <?php endwhile; ?>
    <?php } elseif ( is_page('Stallion') ) { ?>
    <h3>Read News about our Stallion</h3>
    <?php
    $recentstallion = new WP_Query();
    $recentstallion->query('category_name=Stallion&showposts=3');
    ?>
    <?php while ($recentstallion->have_posts())
    : $recentstallion->the_post();
    ?>
    <div id=excerpt-mini-loop>
    <h3><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?>
    <p><a href=\"<?php the_permalink(); ?>\">Read the rest of this story...</a></p>
    </div>
    <?php endwhile; ?>
    <?php } else { ?>
    <?php } ?>
    <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>


    Thanks so much!
  • What is currently happening with this page? What is broken?
  • Hey Ashton,

    Ha! That might have helped huh? Sorry...
    Nothing is being displayed. It is like the code isn't even there.
    I have triple checked all of the page and cat names to make sure this wasn't the issue.
    The mini loops should work, because I copy / pasted them from another area of my site where they do work, changed the cats obviously. I suspect it is something with my conditional statement?

    Cheers!
    Chris
  • Okay, Here's some questions:

    Is the header, footer and sidebar showing up?
    What is the name of that file? (index.php?)
    What page are you going to get this page to display?

    One possibility is a messup with the hierarchy... are you familiar with this: http://codex.wordpress.org/Template_Hierarchy ?
  • Hey Ashton,

    Is the header, footer and sidebar showing up?


    Yep, the page loads as if nothing has changed. I made sure to disable cache when testing this...

    What is the name of that file? (index.php?)


    It is my page.php

    Notes:
    I have a number of *static* pages plus several hundred posts across 6 categories. I am trying to show the 3 most recent posts from a specific category at the bottom (after the loop) of each of these pages, a different category will be used for some pages. Some pages will show the same loop (once I get this issue figured out I will add (array) into the conditional statement to handle that.

    What page are you going to get this page to display?


    See notes above, I think the easiest way to describe what I am trying to do is:

    Below the main content of the displayed *static* page...
    If on page "about" grab the 3 latest posts from category "News" and display them as excerpts, else if on page "whatever" get the 3 latest from category "B" and display as excerpts, else if on page "somesuch" get the 3 latest from category C and display as excerpts, any other page, do nothing.

    I don't want this in a sidebar... I would like it to display in the same div / column as the content of the static page...

    Make sense?
  • Hmmmm... Okay so it sounds like you've got the hierarchy set up correctly.

    The next thing I'd check is your if statements:

    is_page('Training')

    Are you sure this is working? Have you tired using the Page ID instead of the page name?
  • Hey Ashton,

    Yeah, I tried page name, slug and ID# as I understand all 3 should work. I focused on that first too as a simple typo would break it. I do believe it is the conditional statement though. I tried the mini loops without the if statement and they displayed as expected. I then took the loops out and replaced them with text in <h2> tags, you know, if this page display this text etc. Again, these <h2> text did not show

    So this leads me to believe the issue is the statement? Yes?
  • "cjk" said:
    So this leads me to believe the issue is the statement? Yes?


    Yes.

    Did you try the ID with single quotes around it?
  • Hey Ashton,

    Yes, always single, regular up/down quotes wrapping page name, or slug, or ID. But, should it not work with any of these, the same?
  • It should work with any of these. I have had similar trouble with this is_page() function before and I just had to fiddle with it until it worked. So that's what I recommend... lol
  • "AshtonSanders" said:
    It should work with any of these. I have had similar trouble with this is_page() function before and I just had to fiddle with it until it worked. So that's what I recommend... lol


    Ain't that the truth...

    I just retyped it from scratch and it worked as advertised... obviously there is an error in the syntax of one line but for the life of me I can't find it!

    SOLVED!!! Don't know why, but it just is!

    Cheers!
    Thanks for your support!