Support » Fixing WordPress » Displaying Text based on Parent Page’s Name/Slug

  • Hi all, just looking to find out how to specify a bit of text to be displayed if the parent page’s name equals a certain value. Currently I have a tree like this:

    Attorneys

    Houston
    Attorney 1
    Attorney 2

    Austin
    Attorney 3
    Attorney 4

    What I’m looking to do is when the attorney page (which has it’s own template) is called, it checks to see what the parent page’s name is (in this cause Houston or Austin) and displays a block of text that I define in the template.

    I’ve been working a bit with conditionals, so I’m pretty squared away on that, but I just can’t seem to figure out how to reference the page name correctly.

Viewing 8 replies - 1 through 8 (of 8 total)
  • See: http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

    Specifically this bit with the parent page’s ID (not name):

    $post->post_parent == '2'

    Okay, so I can’t have it get the page name then? :-/ Drat.

    Well, I found out that the argument only works for the subpage of a page, and not for the subsubpage of a subpage.

    main-1
    subpage-1
    subpage-2
    subsubpage-1
    subpage-3
    main-2
    subpage-4
    subpage-5

    When I’m at subsubpage-1, the parent page id returned to be the subpage-2. Is there any ways to make return the parent page id to be the global parent page, in this case, the main-1 page?

    The following chunk of code should do what you’re looking for, faisalmalik, and put the result in $globalParentPage:

    <?php
    $currPost = $post;
    while ($currPost->post_parent) { $currPost = $currPost->post_parent; }
    $globalParentPage = $currPost;
    ?>

    Onebrightlight, I realize it’s been several months since you posted your question, but the following should do what you want, and place the result in $parentTitle:

    <?php
    $currPost = $post;
    if ($currPost->post_parent) { $currPost = $currPost->post_parent; }
    $parentTitle = get_the_title($currPost);
    ?>

    Hey folks – glad I found this topic (after searching for what feels like hours!), b/c I have a similar need.

    I basically want to grab the slug of a sub-page’s parent. This will then be used in a dynamic menu highlight scheme…

    So far I’ve found methods for calling the post’s slug, as well as getting the parent’s ID # – but NOT the actual slug of the parent. For example:

    <?php echo $post->post_name; ?> – returns the post’s slug

    <?php echo $post->post_parent; ?> – returns parent’s post id#

    Now the second code example timjpriebe posted above is the closest I’ve found to what I need — falls just short, as it returns (best I can tell) not the actual slug but the parent’s post title.

    So… any help on perhaps tweaking that code or some other method for getting the parent’s slug?

    OK – hours of searching later (this time, I’m not just “feeling” that way!), I’ve found the following code at Perishable Press (see “Method #8” described on that linked page):

    <?php
    	$current_page = $post->ID;
    	$parent = 1;
    
    	while($parent) {
    		$page_query = $wpdb->get_row("SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    		$parent = $current_page = $page_query->post_parent;
    		if(!$parent) $parent_name = $page_query->post_name;
    	}
    ?>

    End result is that $parent_name gets the parent’s slug. Bravo.

    Now… can anybody help me understand WHY/HOW this works? 😉 (jk… sorta…)

    i think it’s easiest way to get parent’s slug, if that’s the case here.

    # current page slug
    $slug = $post->post_name;
    
    # get parent page's slug
    $post_data = get_post($post->post_parent);
    $slug = $post_data->post_name;

    Spent ages looking for how to display the parents children NOT current page children on a consistent menu across a portfolio site.

    This post helped

    Thanks

    $parent_id = $post->post_parent;
    wp_list_pages(“title_li=&child_of=72&iddepth=1”);

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Displaying Text based on Parent Page’s Name/Slug’ is closed to new replies.