WordPress.org

Forums

[resolved] Inline PHP vs functions.php (2 posts)

  1. leMaxim
    Member
    Posted 7 years ago #

    When I put
    <?php echo get_the_title($post->post_parent) ?>
    into my theme template the output is correct.

    However, if I try to put it into a function and then call it from functions.php it does not work. Are there some sort of global variables that I should be calling?

    I'm trying to use this with conditional parameters in the functions.php file because the template itself would get quite bloated if I had it inline.

    Thanks in advance for any help!

  2. When I put
    <?php echo get_the_title($post->post_parent) ?>
    into my theme template the output is correct.

    However, if I try to put it into a function and then call it from functions.php it does not work. Are there some sort of global variables that I should be calling?

    Should be obvious, no? You are directly using the global variable $post (you're trying to access the $post->post_parent variable). So you need to put global $post; at the top of your function.

    function whatever()
    {
    global $post;
    echo get_the_title($post->post_parent);
    ...

Topic Closed

This topic has been closed to new replies.

About this Topic