Search code examples
wordpressif-statementgenesis

Wordpress Genesis, if on a blog post


I am trying to add conditional logic to blog posts, but I cannot work out how to do that. Both

if ( is_singular( 'post' ) ) {

and

if ( is_single() ) {

return false


Solution

  • Ok, after reading your comment i know why it isn't working. You cannot use it directly in functions.php.

    Why
    functions.php runs way before the is_single() method is available.

    How to use it

    • Use it in functions that are loaded by hooks, more info about WP action hooks here.
    • You can also use it directly in page templates. I.e. single.php etc.

    Example
    You can place this directly in your functions.php

    add_action('wp','testing_is_single_method');
    function testing_is_single_method() {
      if(is_single()) {
        add_action('wp_footer', function() {
        ?>
        <script type="text/javascript">alert('HEY THIS IS A SINGLE (BLOG) POST');</script>
        <?php
        });
      }
    }
    

    Regards, Bjorn