WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Get parent page title, or if no parent, get current page title.

[Resolved] Get parent page title, or if no parent, get current page title.

  • Hello again guys.
    Ok this is a strange question, because you wouldn’t normally want this, but here goes.

    I’m looking for some code that is basically an if function:
    Page has parent? YES: Get parent title. NO: Get current page title.

    It sounds simple to me, but I’m having trouble intergrating the two.

    Here’s what I’ve got so far. This is to call just the parent title (so it only works if I’m on a sub page):

    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>

    Anyone know how to create the whole function?

    Thanks in advance. Also, my site if anyone’s interested: http://www.pullupbarsx.com

    [No bumping, please.]

Viewing 1 replies (of 1 total)
  • SOLVED. The answer is

    <?php
    echo empty( $post->post_parent ) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );
    ?>

    if you were wondering.

    Good luck guys!

Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Get parent page title, or if no parent, get current page title.’ is closed to new replies.
Skip to toolbar