WordPress.org

Ready to get started?Download WordPress

Forums

if is a page, or a child of a page, or in a category (a solution) (6 posts)

  1. dressedinvalue
    Member
    Posted 5 years ago #

    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!

  2. desmondo
    Member
    Posted 5 years ago #

    That worked a treat for me! Thanks for posting that up.

  3. trex33
    Member
    Posted 5 years ago #

    Excellent one! Thanks for adding this.

  4. taghaboy
    Member
    Posted 4 years ago #

    So good idea, perfect.
    Thanks

  5. cblock
    Member
    Posted 4 years ago #

    i am having a similar problem:
    My conditional statement works for all cases except arts-entertainment page. Below is my code, any help would be appreciated.

    ===================

    <?php
    if ( in_category('news') || post_is_in_descendant_category( 13 ) ) {
    include 'includes/nav-home.php';
    }
    elseif ( in_category('business') || post_is_in_descendant_category( 14 ) || is_page('business')) {
    include 'includes/nav-bus.php';
    }
    elseif ( in_category('arts-entertainment') || post_is_in_descendant_category( 17 ) || is_page('arts-entertainment')) {
    include 'includes/nav-ent.php';
    }
    elseif ( in_category('living') || post_is_in_descendant_category( 19 ) || is_page('living')) {
    include 'includes/nav-liv.php';
    }
    elseif ( in_category('opinion') || post_is_in_descendant_category( 18 ) || is_page('opinion')) {
    include 'includes/nav-opn.php';
    }
    elseif ( in_category('sports') || post_is_in_descendant_category( 20 ) || is_page('sports')) {
    include 'includes/nav-spt.php';
    }
    else {
    include 'includes/nav-home.php';
    }
    ?>

    ==================
    FYI: The idea is to use a different color for navigation for each page in the conditional statement.

  6. TonyGeer
    Member
    Posted 4 years ago #

    dressed - this is exactly what I was looking for, thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic