• I’ve been reading around a lot of the discussion following sub pages and sub-sub pages, but I haven’t quite found the answer I’m looking for (most of the posts seem to concern wp_list_pages showing a proper nav list when on page, child, or sub-child…)

    I think I know roughly how you would do this in plain English, but my PHP isn’t that strong.

    Problem

    I need to define a variable for templating reasons that if you are on a page, any of its sub-pages, or any of those sub-page’s sub-pages.. and so on.

    Following the codec example I have thus far got:

    <?php
    
    if 	(is_page('20') || $post->post_parent=="20") {
    	$sidebar_call = 1;
    } elseif (is_page('34') || $post->post_parent=="34") {
    	$sidebar_call = 2;
    } elseif (is_page('11') || $post->post_parent=="11") {
    	$sidebar_call = 3;
    } elseif (is_page('12') || $post->post_parent=="12") {
    	$sidebar_call = 4;
    } else {
    	$sidebar_call = "default";
    }	
    
    ?>

    As I expected, this works fine on any of the above pages or their sub-pages, but when you get to a sub-sub-page the variable is assigned the default value.

    I can imagine that I need to create an array (as can now be done in 2.5), but am not sure how to go about it.

    I know there is a function for get_children, but I haven’t found any documentation on how that works (and whether it gets children of the children!).

    My English version of the code would be:

    <?php
    
    if 	(is_page('20') || (is_page(get an array of all children and sub children of page 20...) {
    	$sidebar_call = 1;
    } elseif (is_page('34') || (is_page(get an array of all children and sub children of page 34...) {
    	$sidebar_call = 2;
    } etc...
    
    ?>

    However I’m not sure exactly how to make this happen….

    Any ideas?

Viewing 4 replies - 31 through 34 (of 34 total)
  • Thread Starter alexleonard

    (@alexleonard)

    Hmm, I had hoped that WordPress’ new template page – image.php – would behave similarly to single.php in this situation, but unfortunately not.

    I applied the same function, eval_post, to the image.php but it’s just falling back to the default sidebar.

    I’m going to have to do some reading up to find out how you can find out what category an image belongs in – hopefully the image.php page will somehow be attached to the post or page id that you originally placed the gallery on.

    If anyone has any suggestions, please let me know. If I work anything out I’ll report back here.

    Thread Starter alexleonard

    (@alexleonard)

    Hurrah for reusable functions!

    With a little bit of tweaking I’ve got this functioning on another site that’s under development.

    https://url.ie/jvp

    Thread Starter alexleonard

    (@alexleonard)

    And the original site in question is now live! Although if anyone has an idea how to relate a post attachment to it’s post that would be great!

    https://url.ie/l1p

    Thread Starter alexleonard

    (@alexleonard)

    I’ve been playing around with this some more for another site and needed something a little simpler which wouldn’t require predefined class names – instead something just taking the class name from the post slug.

    Of course I could just do:

    <body class="<?php echo $post->post_name; ?>

    and this, I believe would place the post slug as a class attribute in the body element.

    However I needed something that would do that, but in the case of a “page” which was a child, I wanted to always return the page-parent as the class.

    So I’ve modded the function to read as follows:

    function eval_page($pageid) {
            global $pageClass;  //global, because it's just that useful.
            $pageClass = "default";  // Set the page type as default
            while ($pageid) {
                $page = get_post($pageid);
                $pageid = $page->post_parent;
                $pageClass = $page->post_name;
            }
            $pageClass = "page-" . $pageClass;
            return $pageClass;
        }

    This can be used in your header.php section as follows:

    <body class="<?php echo eval_page($post->ID); ?>">

    Having this extra class in the body element just allows for some extra CSS styling on specific pages.

    Hope it’s useful to someone.

Viewing 4 replies - 31 through 34 (of 34 total)
  • The topic ‘More fun with sub sub pages’ is closed to new replies.