WordPress.org

Ready to get started?Download WordPress

Forums

Parent page title on subpage (11 posts)

  1. chocula
    Member
    Posted 1 year ago #

    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

  2. David Gard
    Member
    Posted 1 year ago #

    When you are in The Loop (Codex) you can simply output the 'post_parent' value -

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

  3. chocula
    Member
    Posted 1 year ago #

    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?

  4. David Gard
    Member
    Posted 1 year ago #

    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) :

  5. chocula
    Member
    Posted 1 year ago #

    brilliant, thank you very much

  6. chocula
    Member
    Posted 1 year ago #

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

  7. David Gard
    Member
    Posted 1 year ago #

    What PHP error?

  8. chocula
    Member
    Posted 1 year ago #

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

  9. David Gard
    Member
    Posted 1 year ago #

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

  10. chocula
    Member
    Posted 1 year ago #

    $post_parent = get_post(80);

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

  11. David Gard
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic