Support » Developing with WordPress » get_post_format & has_post_format problem

  • skromta

    (@skromta)


    I hope this is the right place to ask this question. I don’t know much about WP and I really don’t know much about PHP (although I know several other languages) so I’m probably missing something obvious.

    Anyway, I want to modify a theme to not output the title for status and photo posts. So at the appropriate place in the theme (yes I use a child theme) I added an if-statement with a has_post_format call. Unfortunately, this didn’t work and I started to try to figure out why by using get_post_format to see what post format the post have (this is for a single post). I used this snippet (CSS removed)

    <h1><?php the_title(); ?> XXX <?php get_post_format(); ?> YYY</h1>

    And I end up with this output “Current status XXX YYY”. Slightly confused, this post has the “photo” post format not the “standard” format, I changed the line to see if I needed to use the post ID

    <h1><?php the_title(); ?> XXX <?php get_post_format(the_ID()); ?> YYY</h1>

    And I get “Current status XXX 38 YYY” … which makes me even more confused. What am I missing here?

Viewing 2 replies - 1 through 2 (of 2 total)
  • You need to understand the difference between a function returning a value, and echoing a value.

    the_title() echoes the title, meaning that it prints the title to the page as soon as the function executes. It’s generally the convention in WordPress that functions starting with the_ echo their output.

    get_the_title() returns the value, meaning that you can use it to pass the title into a variable, or into another function, without printing it to the page.

    So, in your template, when you write:

    <?php get_post_format(); ?>
    

    You’re returning the post format to… nothing. It’s just getting post format and throwing it out into the ether. WordPress doesn’t have a the_post_format() function, because it’s not a value that you normally need to output, so to print it to the page you need to echo the value it returns, like so:

    <?php echo get_post_format(); ?>
    

    That will show you the post format.

    The problem with <?php get_post_format(the_ID()); ?> is that the_ID() echoes the ID, while get_post_format() returns the post format. So essentially it’s just printing the ID. If you need to pass an ID to get_post_format(), use the get_the_ID() function:

    <?php echo get_post_format( get_the_ID() ); ?>
    

    The get_ vs the_ rule is a sort-of useful shorthand for WordPress, but not a perfect rule (some get_ functions have options for echoing, for example). The only way to know for sure whether a function echos or returns is to experiment or refer to the documentation. WordPress’ function documentation is here: https://developer.wordpress.org/reference/functions/

    skromta

    (@skromta)

    Ahhh, thanks. I had no idea about the echo/non-echo thing … I just saw “returns string”.

    Thanks for the help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get_post_format & has_post_format problem’ is closed to new replies.