WordPress.org

Support

Support » How-To and Troubleshooting » Parent page title on subpage

Parent page title on subpage

  • chocula
    Member

    @chocula

    So my site structure looks like this..

    -News
    -Work
         -work1                    -work2   -work 3
            -subwork1  -subwork2
    -Contact

    What i want to do it show the parent page title “work1” on the subpage “subwork1, subwork 2 etc”…I may want to extend this to another category in the future. Can anyone give me a pointer how to do this?

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • David Gard
    Member

    @duck_boy

    When you are in The Loop (Codex) you can simply output the ‘post_parent’ value –

    <?php echo $post->post_parent; ?>

    chocula
    Member

    @chocula

    Ok thanks for that. How would i do that so it only shows on the child pages of one parent…not all pages that have parents?

    David Gard
    Member

    @duck_boy

    I realiase I msiread your post – my code above will only output the Post Parent ID. This code below (where 99 is the ID of the parent) should do the trick.

    This will show the tile of the Post Parent for ALL pages that are a direct-child of Page ID 99.

    <?php
    if($post->post_parent === 99) :
    
        $post_parent = get_post(99);
        echo $post_parent->post_title;
    
    endif;
    ?>

    To make it so that only one single child shows this, add a condition to check for the ID of the Page that is being shown (where 101 is that ID) –

    if($post->post_parent === 99 && $post->ID === 101) :

    chocula
    Member

    @chocula

    brilliant, thank you very much

    chocula
    Member

    @chocula

    hmm, i tried doing that but i get a php error..i put it in the content-page.php

    David Gard
    Member

    @duck_boy

    What PHP error?

    chocula
    Member

    @chocula

    Fatal error: Only variables can be passed by reference in /website/name/etc/wp-content/themes/etc/content-page.php on line 18

    David Gard
    Member

    @duck_boy

    And which of these lines is line 18 in your code? Also, what version of PHP are you using?

    chocula
    Member

    @chocula

    $post_parent = get_post(80);

    the site is hosted by dreamhost so i imagine it has the latest stable

    David Gard
    Member

    @duck_boy

    Try this –

    $post_parent = get_post($post_id = 80);

    If that fails, place this directly after that line –

    echo '<pre>'; print_r($post_parent); echo '</pre>';

    I wonder if the error is infact being caused by echo $post_parent->post_title; – it could be that the page is not being found, so you are trying to output from an object that does not exist.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Parent page title on subpage’ is closed to new replies.