[resolved] [closed] Get parent page title, or if no parent, get current page title. (2 posts)

  1. jmadz1
    Posted 4 years 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):

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

  2. jmadz1
    Posted 4 years ago #

    SOLVED. The answer is

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

    if you were wondering.

    Good luck guys!

Topic Closed

This topic has been closed to new replies.

About this Topic