WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want to do is to dislay a description of a country if one of the categories is a country name.

It do it like that

if(in_category('afghanistan')) {
    $recent = new WP_Query("page_id=25"); while($recent->have_posts()) : $recent->the_post();?>
        <h3><?php the_title(); ?></h3>
        <?php the_content(); ?>
    <?php endwhile;

elseif(in_category('albania')) {
    $recent = new WP_Query("page_id=26"); while($recent->have_posts()) : $recent->the_post();?>
        <h3><?php the_title(); ?></h3>
        <?php the_content(); ?>
    <?php endwhile;

else {
    echo "Error!";

I would like to replace page_id=25 with pagename=afghanistan. Iif I do so it works well, but if page "afghanistan" is a child of a page "Asia" it doesn't display it anymore. If I use page_id it works, but pagename stops working as soon as I create page as a child of another page. How do I get the page by name if it has a parent? :)

share|improve this question
up vote 0 down vote accepted

When you give a page a parent, the slug of the page becomes %parent-pagename%/%pagename%, so you should be able to target it with pagename=asia/afghanistan.

share|improve this answer
Thanks a lot! Easy fix, but hard to find – Tomas Mar 2 '12 at 17:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.