WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Echo Root Title

[Resolved] Echo Root Title

  • The following code echos the root page id. How can I get it to echo the root page title instead of the id number? I’d also like to make it a link.

    <?php $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?>
    <h2><?php echo $root_page_id; ?></h1>
Viewing 14 replies - 1 through 14 (of 14 total)
  • <?php echo get_the_title($root_page_id);?>

    That does the trick. On the home page it this code displays the latest post title. I would probably need some type of if statement to detect the homepage and display the site title. What code could I use for that?

    And one more thing. When a post page is displayed the code displays the post title. How could this code display the site title instead of the post title?

    Rajesh Soni
    Participant

    @rajeshsoni

    <?php $page_title = ( empty( $post->ancestors ) ) ? get_bloginfo('name')  : get_the_title(end( $post->ancestors )); ?>
    <h2><?php echo $page_title ; ?></h1>

    I tried the following code to resolve the homepage issue. It gave my site the white screen of death and if I remove the code I can’t get my site back.

    <?php
    if ( is_home() ) {
        <h1><?php bloginfo('name'); ?></h1>
    } else {
        <?php $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?>
        <h1><?php echo get_the_title($root_page_id);?></h1>
    }
    ?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Rajesh Soni
    Participant

    @rajeshsoni

    <?php
    if ( is_home() ) {
        ?><h1><?php bloginfo('name'); ?></h1><?php
    } else {
        $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors ); ?>
        <h1><?php echo get_the_title($root_page_id);?></h1>
    }
    ?>

    I’m banging my head over this one. I get a white screen with the following code. I don’t see any issues with it.

    <?php
    if ( is_home() ) {
    // This is a homepage
    <h1>Homepage</h1>
    } else {
    // This is not a homepage
    <h1>Not Homepage</h1>
    }
    ?>
    Andrew Nevins
    Forum moderator

    @anevins

    You aren’t closing off your PHP or stringifying the HTML.

    Rajesh Soni
    Participant

    @rajeshsoni

    Why aren’t you using the code I suggested?
    Why do you keep changing on your own?

    I tried, but my site displays a white screen with the if statement code. Can you post an example of what it should look like? Is ?> not properly closing the php?

    Rajesh Soni
    Participant

    @rajeshsoni

    I already did twice, see above.

    Andrew Nevins
    Forum moderator

    @anevins

    Go with Rajesh’s example.

    Rajesh your first snippet of code works perfectly. I’m not sure why it wasn’t displaying correctly…user error!

    Rajesh Soni
    Participant

    @rajeshsoni

    Awesome!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Resolved] Echo Root Title’ is closed to new replies.