Grow your CSS skills. Land your dream job.

[Solved] WordPress Conditional Statement – Mini Loop Help!

  • cjk
    # January 8, 2010 at 9:47 am

    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?

    Code:
    < ?php get_header(); ?>

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

    < ?php the_content('

    Read the rest of this page »‘); ?>

    < ?php endwhile; endif; ?>
    < ?php edit_post_link('Edit this entry.', '

    ‘, ‘‘); ?>

    < ?php if ( is_page('About') ) { ?>

    Read The Latest News

    < ?php
    $recentnews = new WP_Query();
    $recentnews->query(‘category_name=News&showposts=3′);
    ?>
    < ?php while ($recentnews->have_posts())
    : $recentnews->the_post();
    ?>

    < ?php endwhile; ?>
    < ?php } elseif ( is_page('Lessons') ) { ?>

    Read Articles about Lessons

    < ?php
    $recentlessons = new WP_Query();
    $recentlessons->query(‘category_name=Lessons&showposts=3′);
    ?>
    < ?php while ($recentlessons->have_posts())
    : $recentlessons->the_post();
    ?>

    < ?php endwhile; ?>
    < ?php } elseif ( is_page('Training') ) { ?>

    Read Training Articles

    < ?php
    $recentarticles = new WP_Query();
    $recentarticles->query(‘category_name=training-articles&showposts=3′);
    ?>
    < ?php while ($recentarticles->have_posts())
    : $recentarticles->the_post();
    ?>

    < ?php endwhile; ?>
    < ?php } elseif ( is_page('Showing') ) { ?>

    Read Articles on Showing

    < ?php
    $recentshowing = new WP_Query();
    $recentshowing->query(‘category_name=showing-articles&showposts=3′);
    ?>
    < ?php while ($recentshowing->have_posts())
    : $recentshowing->the_post();
    ?>

    < ?php endwhile; ?>
    < ?php } elseif ( is_page('Stallion') ) { ?>

    Read News about our Stallion

    < ?php
    $recentstallion = new WP_Query();
    $recentstallion->query(‘category_name=Stallion&showposts=3′);
    ?>
    < ?php while ($recentstallion->have_posts())
    : $recentstallion->the_post();
    ?>

    < ?php endwhile; ?>
    < ?php } else { ?>
    < ?php } ?>
    < ?php get_sidebar(); ?>

    < ?php get_footer(); ?>

    Thanks so much!

# January 9, 2010 at 12:48 am

What is currently happening with this page? What is broken?

cjk
# January 9, 2010 at 9:55 am

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

# January 9, 2010 at 12:42 pm

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 ?

cjk
# January 9, 2010 at 3:47 pm

Hey Ashton,

Quote:
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…

Quote:
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.

Quote:
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?

# January 9, 2010 at 5:04 pm

Hmmmm… Okay so it sounds like you’ve got the hierarchy set up correctly.

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

Quote:
is_page(‘Training’)

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

cjk
# January 9, 2010 at 6:12 pm

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?

# January 9, 2010 at 6:20 pm
"cjk" wrote:
So this leads me to believe the issue is the statement? Yes?

Yes.

Did you try the ID with single quotes around it?

cjk
# January 10, 2010 at 2:12 pm

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?

# January 10, 2010 at 2:24 pm

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

cjk
# January 10, 2010 at 10:34 pm
"AshtonSanders" wrote:
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!

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".