WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Echo Root Title (15 posts)

  1. ajcke
    Member
    Posted 1 year ago #

    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>
  2. dotnordic
    Member
    Posted 1 year ago #

    <?php echo get_the_title($root_page_id);?>

  3. ajcke
    Member
    Posted 1 year ago #

    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?

  4. ajcke
    Member
    Posted 1 year ago #

    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?

  5. Rajesh Soni
    Member
    Posted 1 year ago #

    <?php $page_title = ( empty( $post->ancestors ) ) ? get_bloginfo('name')  : get_the_title(end( $post->ancestors )); ?>
    <h2><?php echo $page_title ; ?></h1>
  6. ajcke
    Member
    Posted 1 year ago #

    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.]

  7. Rajesh Soni
    Member
    Posted 1 year ago #

    <?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>
    }
    ?>
  8. ajcke
    Member
    Posted 1 year ago #

    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>
    }
    ?>
  9. Andrew
    Forum Moderator
    Posted 1 year ago #

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

  10. Rajesh Soni
    Member
    Posted 1 year ago #

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

  11. ajcke
    Member
    Posted 1 year ago #

    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?

  12. Rajesh Soni
    Member
    Posted 1 year ago #

    I already did twice, see above.

  13. Andrew
    Forum Moderator
    Posted 1 year ago #

    Go with Rajesh's example.

  14. ajcke
    Member
    Posted 1 year ago #

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

  15. Rajesh Soni
    Member
    Posted 1 year ago #

    Awesome!

Topic Closed

This topic has been closed to new replies.

About this Topic