WordPress.org

Ready to get started?Download WordPress

Forums

Get Parent Pages (10 posts)

  1. spiralstarez
    Member
    Posted 6 years ago #

    Greetings,

    I have about 7 parent pages each with children, and children of those children on a few of the pages. I'm using list_pages to output them into an unordered list, and then styling them with CSS to make navigation.

    For each of the parent pages, and also on its children, I want to have a different header image.

    Is there an easy way to do this, without making and assigning a template to each parent and its children?

    Regards,

    JP

  2. JoshuaIsaac
    Member
    Posted 6 years ago #

    You would probably make a series of if/else statements like follows:

    <?php
    
    	$this_category = get_category($cat);
    
    	$post = $wp_query->post;
    
    		if (in_category('22')) {
    			include(TEMPLATEPATH . '/header-home.php');
    		} elseif ( in_category('9')) {
    				include(TEMPLATEPATH . '/header-about.php');
    			} else {
    			include(TEMPLATEPATH . '/header-generic.php');
    			}
    		};
    
    	?>

    You would place this in your header.php file and then place the other files header-home.php, header-about.php, and header-generic.php in your theme directory.

  3. jeremyduffy
    Member
    Posted 6 years ago #

    Are you talking about something like what I have? Take a look:

    http://www.jeremyduffy.com/sitemap

    Click on one of the categories and you'll see a custom image per category. For the one's with subcategories (like tools which has firefox plugins as a subcategory), clicking on the subcategory has a different header image as well.

    All category and subcategory pages are run with a single tempalte file.

  4. jeremyduffy
    Member
    Posted 6 years ago #

    Anyway, the way that I did it was to extract the postname of the current page and use that as the image name for the header. So if I'm on the "Scams and Ripoffs" page and it's $post-post_name is "scams", the header image will be /images/scams.jpg or something like that. Contact me from my page if you have more questions.

  5. spiralstarez
    Member
    Posted 6 years ago #

    thanks, that sounds like it works on a per page basis, however I'm looking to have something like this:

    home
    about
    - about1
    -- about1.1
    -- about1.2
    --- about 1.2.1
    --- about 1.2.2
    contact

    so on the about page, If I'm in 1.2.1 or 1.1 or any of the pages under the parent 'about' page, I want to show one header image for the parent and all its children.

    I've seen in some code that uses a while loop to check each $page if it has_parent or something like that, and get it into an array. That seems like a good option to achieve this, however my PHP and wordpress knowledge aren't good enough yet to achieve it.

    Ultimately I think I'm just going to choose 7 images or so and use PHP random to switch them as it's not imperative that they are specific to a page though it would be nice for future projects to know how to do it.

  6. spiralstarez
    Member
    Posted 6 years ago #

    I've been looking into the code in the breadcrumb plugin to examine how this would be possible. This code obviously is able to retrieve the parent pages, and I imagine it's just a matter of retrieving all the pages that don't have children in the site and stuffing them into an array called $parents. I'm not sure how to translate what is here into what I am after yet though, but for anyone else wanting similar functionality I think this would be a good starting point.

    function get_breadcrumb_page($breadcrumb, $params)
    {
      global $wp_query;
    
      if ($wp_query->is_page)
      {
        $object = $wp_query->get_queried_object();
    
        // Parents.
        $parent_id  = $object->post_parent;
        $parents    = array();
        while ($parent_id)
        {
          $page       = get_page($parent_id);
    
          if ($params["link_none"])
            $parents[]  = get_the_title($page->ID);
          else
            $parents[]  = '<a href="'.get_permalink($page->ID).'" title="'.get_the_title($page->ID).'">'.get_the_title($page->ID).'</a>';
    
          $parent_id  = $page->post_parent;
        }
    
        // Parents are in reverse order.
        $parents    = array_reverse($parents);
        $breadcrumb = array_merge($breadcrumb, $parents);
    
        // Current page.
        if ((breadcrumb_is_paged() || $params["link_all"]) && !$params["link_none"])
          $breadcrumb[] = '<a href="'.get_permalink($object->ID).'" title="'.get_the_title($object->ID).'">'.get_the_title($object->ID).'</a>';
        else
          $breadcrumb[] = get_the_title($object->ID);
      }
    
      return $breadcrumb;
    }
  7. cacycleworks
    Member
    Posted 6 years ago #

    In the event Google brings more visitors here, I am doing the exact same thing as spiralstarez only the purpose is to prevent XSS injection into the URL of wordpress pages. So I need to know what I think my permalink should be before I call get_header();. :-(

    This handy gem will get you for pages what get_category_parents() does for posts. I guess I should have called it get_page_parents()... :)

    function page_ancestory( $id=0, $separator="/" ){
    	$itisme=get_post($id);
    	$lineage=$itisme->post_name;
    	$parentID=$itisme->post_parent;
    	while( $parentID != 0 ){
    		$parent=get_post($parentID);
    		$lineage=$parent->post_name.$separator.$lineage;
    		$parentID=$parent->post_parent;
    	}
    	return $lineage;
    }
    echo page_ancestory($post->ID);

    In spiralstarez's case, the output would be:

    about/1/1.1/1.2/1.2.1/1.2.2

    Assuming that the above "words" are page "slugs".

    OH! I forgot to mention that $post works anywhere on a "page" template for me, so doesn't need to be in the loop.
    Reference: codex for get_post()

    Also, be aware that $post->post_content IS the whole page (or post) itself, so this child of the object can be bigger than you might think ... and spools out ALL of that html and if you're not paying attention, you think something broke, when it actually didn't.

    To see what's going on with an array or object, I will do this:

    <pre><?
    print_r($post);
    ?></pre>

    But in our case, this makes the "page" twice as long and the only hint is the preformatting of the "post_content". To make things better, if I am working outside the loop, I will often do this:

    <pre><?
    unset($post->post_content);
    print_r($post);
    ?></pre>

    Which will make the output more friendly:

    [ID] => 146
    [post_author] => 1
    [post_date] => 2007-12-12 12:53:28
    [post_date_gmt] => 2007-12-12 20:53:28
    [post_content] =>
    [post_title] => Terms & Conditions
    [post_category] => 0
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => open
    [ping_status] => open
    [post_password] =>
    [post_name] => terms-conditions
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2007-12-14 14:47:54
    [post_modified_gmt] => 2007-12-14 22:47:54
    [post_content_filtered] =>
    [post_parent] => 6
    [guid] => http://qa.mydomain.com/wordpress/policies/terms-conditions
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] =>
    [comment_count] => 0

    And from seeing this, you can see that this post's parent is policies (from the guid) and policies's ID is 6.

    :) Chris

  8. natalie
    Member
    Posted 6 years ago #

    You know, this might be easier, if all you want to do is use CSS to do this, much cleaner too. What I do is just add an id or class to the <body> tag and then style each page differently. That would mean instead of a physical header img (<img src...) you'd make it a background image with CSS.

    You can read more here... http://wp.nataliejost.com/dev/body-ids-classes/

  9. moshu
    Member
    Posted 6 years ago #

    You are absolutely wrong... or you just don't get the question discussed in this topic.
    By your method, if I have 57 sub-Pages I will have to add 57 conditionals into the template file. That's crazy!

    What was discussed above was a method to be able to assign programatically the same template to the children/sub Pages as their parent has.
    The poster above you also completely misunderstood the original question.

    Anyway, stop digging out months old posts to promote your site!

  10. natalie
    Member
    Posted 5 years ago #

    Oh, hi moshu, guess I caught you in a foul mood that day. I wasn't trying to promote anything, the site's actually a private site I only show people when I happen to have a solution that might work. I was doing some research on this for myself, relating to a similar but different issue, and came across this post - thought I could help. Obviously I misunderstood what was trying to be accomplished, but there's no need to be nasty. I admit I didn't read all of the responses, just the initial question posed.

    And yes, of course, if you need something different for each of 57 pages, that would probably require a lot more work, although you wouldn't need 57 conditionals, just one conditional that outputs the page slug or id. But in most cases this is never something I would need to do, so I'll stay out of this one! :)

Topic Closed

This topic has been closed to new replies.

About this Topic