WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Any subpage of a specific parent id (12 posts)

  1. Stefan Weise
    Member
    Posted 3 years ago #

    Ok, forgive my sloppy code - i'm not a PHP programmer, but i've managed to get by with many of the tutorials and suggestions posted here, but this has me in a bind:

    I have the following structure:

    Glossary (ID = 74)
    Glossary Category (Child Page)
    Letter Page (Grandchild Page)
    Term (Great-Grandchild Page)

    What i want is for the Glossary Category title to appear on any child, grandchild and great-grandchild page of "Glossary", or ID 74. Here's what i have so far:

    <?php } else if ( '74' == $post->post_parent || strpos($_SERVER['REQUEST_URI'],'/glossary/')!==false ) { ?>
    <?php
    	$current = $post->ID;
    	$parent = $post->post_parent;
    	$grandparent_get = get_post($parent);
    	$grandparent = $grandparent_get->post_parent;
    ?>
    <h1><?php if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) { echo get_the_title($parent); } else { echo get_the_title($current); }?></h1>

    Here's what happens (Page: Title) with the above code:
    Glossary: Glossary
    Glossary Category: Glossary Category Title
    Letter Page: Glossary Category Title
    Term: Letter Page Title << i want this to also be Glossary Category Title.

    Hopefully i've detailed this enough. Any help is greatly appreciated.

  2. Stefan Weise
    Member
    Posted 3 years ago #

    Here's a friendly "bump" :-)

    Anyone?

  3. Stefan Weise
    Member
    Posted 3 years ago #

    Can someone help me with this please?

  4. Stefan Weise
    Member
    Posted 3 years ago #

    Anyone home? Can someone please help me with this?

  5. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Function_Reference/get_post_ancestors
    http://php.net/manual/en/function.in-array.php

    if( in_array( 74, get_post_ancestors($post->ID) ) ) { echo 'descendant of 74'; }

  6. Stefan Weise
    Member
    Posted 3 years ago #

    Thanks, but i'm not sure where i'm supposed to put that code....

  7. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    it is a code that tests whether the current page is a descendant of page 74;
    where you put that code depends what you want do with that information.

    i don't know if and how this would relate to the code section that you posted.

  8. Stefan Weise
    Member
    Posted 3 years ago #

    Thanks, but that won't work for me. I'm trying to get the child page of 74 and all of the child and grandchild pages of that child page to display the title of the child page of 74. I know, that was some english, but this is confusing the heck outta me

  9. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    theoretical, there might be more than one child page of page74 -

    what you are talking about, is this a special child page of page74?

    if so, it should have a fixed page id, which you can use with the conditional statement.

    somehow, there seems to be mix of pages, categories, terms ... in your idea.

    are you talking about PAGE as in static page (http://codex.wordpress.org/Pages) - or are you talking about web pages?

  10. Stefan Weise
    Member
    Posted 3 years ago #

    My bad, i didn't make this clear. I'm talking about a WordPress page, not a post or a category. What i'm doing is i'm using pages to build a glossary since i don't know enough PHP to create the letter pages dynamically using a custom post type with taxonomy for the glossary categories. So basically the page structure with my current method is:

    • Glossary (ID=74)
      • Glossary Category (Title: "Glossary Category")
        • A (Title:"Glossary Category")
          • A Term (Wanted Title: "Glossary Title", but currently displays: "A")
        • B
        • C
        • ...

    The client is going to be adding new categories and terms going forward so i can't really restrict this to the "Category" pages, but need to restrict it to ID:74. This way the name of the glossary category is always the title all the way down to the term page.

    I do realize that this is a totally bassackwards way of doing this and that custom post types / taxonomy would be the way to go, but i've yet to find anything where automatic alphabetical pagination is created for all terms within a given taxonomy, so this is the method i'm using for now.

    To further illustrate:

  11. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    much clearer now ;-)

    assuming that '74' Glossary is a top level page:

    this is the code:

    <h1><?php $top_page = 174;
    if( in_array( $top_page, get_post_ancestors( $post->ID ) )  && $post->post_parent != $top_page ) {
    // 'this is descendant of the top_page, but not direct child';
    $child_of_top_parent = array_reverse(get_post_ancestors( $post->ID ));
    echo get_the_title($child_of_top_parent[1]);
    } else {
    echo get_the_title($post->ID);
    } ?></h1>

    breakdown:
    in_array( $top_page, get_post_ancestors( $post->ID ) )
    check if the current page is a descendant of '74', i.e. a sub (sub) page of Glossary;

    $post->post_parent != $top_page
    check that the current page is not the direct child; i.e. that it is not Glossary Category

    $child_of_top_parent = array_reverse(get_post_ancestors( $post->ID ));
    get the ancestor list of the current page; (this is an array of page IDs, starting with the ID of the parent of the current page, going up to the oldest ancestor (which should be '74'); to get the position of the 'next oldest' page, reverse the array

    echo get_the_title($child_of_top_parent[1]);
    use array element 1 (arrays elements start with 0) (that is the direct child genration of '74', i.e. Glossary Category) to print the title

  12. Stefan Weise
    Member
    Posted 3 years ago #

    Sir, you are awesome! That works perfectly. Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic