WordPress.org

Support

Support » Themes and Templates » [Resolved] Inline PHP vs functions.php

[Resolved] Inline PHP vs functions.php

  • 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!

Viewing 1 replies (of 1 total)
  • Samuel Wood (Otto)
    WordPress.org Tech Dude

    @otto42

    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);
    ...
Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] Inline PHP vs functions.php’ is closed to new replies.