• How exactly would I accomplish this in a page template?

    I have a page template where there will be one parent and multiple child pages. In each of the child pages I want the parent page title to be displayed at the top.

    So if the page is a child page, I want it to display the parent title (with link).

    If the the page has no parent, it displays the current title.

    not sure how to go about doing this.

Viewing 14 replies - 1 through 14 (of 14 total)
  • this should be in the loop:

    <?php
    if( $post->post_parent ) { ?>
      <a href="<?php get_permalink( $post->post_parent ); ?>"><?php echo get_the_title( $post->post_parent ); ?></a>
    <?php } else {
      the_title();
    } ?>
    Thread Starter Kerosity

    (@kerosity)

    What do you mean by “in the loop”? (Sorry still kinda new to php/wordpress)

    I copied that into my template file and it didn’t seem to work. The child pages show their own title.

    Try using this:

    if( $post->post_parent != 0 )

    In some circumstances PHP will evaluate a 0 as false, but it’s not guaranteed and should not be relied upon. An if() statement that relies on a 0 being regarded as false can cause problems because the statement thinks that there’s a value for $post->post_parent (even if that value is 0) so it’s true which will give you the wrong result.

    Thread Starter Kerosity

    (@kerosity)

    I put that into the second line of what alchymyth suggest right?

    If so that didn’t seem to work either.

    Then you need to start doing some basic de-bugging. ?? Don’t worry, it’s easier then it sounds.

    Before that line, add this in:

    echo "<p>Parent: '".$post->post_parent."'</p>";

    That will show you what the value is.

    It seems like you’re not using this in the loop, as alchymyth suggested. The Loop in WordPress is where the posts contents are laid out. It gives you some good automatic functions to get the current posts/pages details.

    Another thing that you can do is change a setting in your wp-config.php file while you’re devleoping these changes. You need to change this line:

    define('WP_DEBUG', true);

    That will display any error messages, and most likely give you more idea of what is actually wrong. Of course – always remember to chagne it back to false when you’re finished!

    Thread Starter Kerosity

    (@kerosity)

    Okay started debugging:

    When I put your line of code before it, It displayed “Parent: ” “

    So then I looked into the loop link you sent. If I’m reading it right I should wrap my “post content” with this:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    ~post content here~
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Right? I wrapped the previous code in that and it I get the same results.

    Thread Starter Kerosity

    (@kerosity)

    -edit- nevermind… still trying to figure it out.

    Thread Starter Kerosity

    (@kerosity)

    Okay! I turned on the debug mode (that’s really cool!).

    I get this error on both lines where $post->post_parent exists.

    Here’s the error:

    Notice: Undefined variable: post in /file/path/to/template.php on line 116

    Notice: Trying to get property of non-object in /file/path/to/template.php on line 116

    Notice: Undefined variable: post in /file/path/to/template.php on line 118

    Notice: Trying to get property of non-object in /file/path/to/template.php on line 118

    I’m using a WordPress Framework (Yootheme’s Warp v6) so I’m assuming this has something to do with it…

    That means that where you’re trying to use that code, the $post object hasn’t been created yet (or isn’t available to the local scope of that function). There’s probably ways to get around this depending on just where you’re using that code and how it’s being called. Are you trying to do this in a template file, or in your theme’s functions.php file or somewhere else? If there’s more to it then we’ve seen so far, it might be worth throwing the code up on Paste Bin so we can see what’s going on.

    Thread Starter Kerosity

    (@kerosity)

    There’s a lot more going on! Basically what I did so far was take their “template” file and edit it down to where it’s just displaying the header/footer. Then I was going to put my page template code in the middle.

    https://pastebin.com/dpzXiSDX

    The stuff I’m entering starts at line #112

    If you’re going to use that for only pages, then I’d suggest that you save that file as single-page.php. That way it will be used as the default template for any page in your site. If you’re not using it only for pages, then there’s going to be a whole lot more ocnditional code that you’ll need to add in to cover every differnt senario that you’ll face.

    Looking at your code at line 112, that’s not what you need. At all. The intention of the Loop is to output the content of the page. It’s not there to do nothing like that. I would remove that bit completely as it really doesn’t do anything at all for your code and will only break something down further if it’s left there.

    If you’re doing as I said above and using this a single-page.php then you can add this near the top of your page:

    <?php
        the_post();
    
        // Debugging...
        echo "<p>Post tile: '".$post->post_title."', Parent: '".$post->post_parent."'</p>";
    ?>

    Even though you shouldn’t really need to, that will ensure that you have access to the $post object in that script, and you should be able to set up the title correctly. You’ll see that I’ve added osme debugging code in there to, so feel free to remove that once you can see it working.

    Thread Starter Kerosity

    (@kerosity)

    First off, thanks a ton for all of your help. I really appreciate it!

    This is a custom page template that I’m going to be using for about 40-50 pieces of content on my site (and possibly go from there). So I do want it to be different then the rest of the site, but I used a custom template so I can just easily select it while creating the page/child-pages. (I’m not wrong with this line of thinking am I?)

    -edit- And since I’m using the Warp Framework, it’s very funky how you have to create custom page templates, which is why the pastebin I linked above has all that extra code.

    Thread Starter Kerosity

    (@kerosity)

    So I probably went about 6 pages deep on google searches for the debug notice I got above… after trying (trial/error) a dozen different things… this seems to work:

    <?php
    
    global $post;
    
    if( $post->post_parent ) { ?>
      <a href="<?php get_permalink( $post->post_parent ); ?>"><?php echo get_the_title( $post->post_parent ); ?></a>
    <?php } else {
      the_title();
    } ?>

    All that I did was add global $post; before.

    Can someone explain what is going on so I can know what I’m doing in the future… lol.

    It’s exactly like I said in my earlier post:

    That means that where you’re trying to use that code, the $post object hasn’t been created yet (or isn’t available to the local scope of that function).

    That’s the entire answer.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘If Child Page > Show Parent Title > If No Parent > Show Current Title’ is closed to new replies.