Forums

[resolved] Get parent title, or if no parent, get page title?!?! (3 posts)

  1. jmadz1
    Member
    Posted 11 months ago #

    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

  2. Curtiss Grymala
    Member
    Posted 11 months ago #

    <?php
    echo empty( $post->post_parent ) ? get_the_title( $post->ID ) : get_the_title( $post->post_parent );
    ?>
  3. jmadz1
    Member
    Posted 11 months ago #

    You're a star. Thanks a lot Curtiss! Have a nice day/evening.

Reply

You must log in to post.

About this Topic