if is a page, or a child of a page, or in a category (a solution)
-
I wanted to share this from a website I’m working on right now, just in case anyone else might be looking for such a solution. It may or may not be the best approach, but it works for me, and seems simple enough.
This checks to see if the current screen is a specific page or a child of a specific page or (if a post) is in a specific category — and has different sets of criteria.
So, if one set criteria is matched, it’ll do one thing; if another set is matched, it’ll do another. I used this for different sets of navigation in the header template:
<?php if (is_page('page_name_1') || $post->post_parent=="77" || in_category(3) ) { $children = '<a href="/page_slug_1/">Page One</a><ul><li><a href="/page_slug_2/">Page Two</a></li><li><a href="/page_slug_3/">Page Three</a></li></ul></li>' . wp_list_pages("title_li&child_of=77&echo=0"); } elseif (is_page('page_name_2') || $post->post_parent=="75" || in_category(4) ) { $children = '<a href="/page_slug_2/">Page Two</a><ul><li><a href="/page_slug_1/">Page One</a></li><li><a href="/page_slug_3/">Page Three</a></li></ul></li>' . wp_list_pages("title_li&child_of=75&echo=0"); } elseif (is_page('page_name_3') || $post->post_parent=="73" || in_category(5) ) { $children = '<a href="/page_slug_3/">Commercial</a><ul><li><a href="/page_slug_1/">Page One</a></li><li><a href="/page_slug_2/">Page Two</a></li></ul></li>' . wp_list_pages("title_li&child_of=73&echo=0"); } if ($children) { ?> <ul id="suckerfishnav"> <?php echo $children; ?> </ul> <?php } ?>
And I used this in the page and single post templates to show posts from a certain category depending on what criteria was matched:
<?php if (is_page('page_name_1') || $post->post_parent=="77" || in_category('3')) { $my_query = new WP_Query('category_name=cat_slug_name3'); } elseif (is_page('page_name_2') || $post->post_parent=="75" || in_category('4')) { $my_query = new WP_Query('category_name=cat_slug_name4'); } elseif (is_page('page_name_3') || $post->post_parent=="73" || in_category('5')) { $my_query = new WP_Query('category_name=cat_slug_name5'); } while ($my_query->have_posts()) : $my_query->the_post(); ?> <div class="post"> <div class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <div class="serif"><em><?php the_meta(); ?></em></div> </div> <?php endwhile; ?>
I hope this helps someone!
- The topic ‘if is a page, or a child of a page, or in a category (a solution)’ is closed to new replies.