Support

Account

Home Forums General Issues Help! True / False to include or exclude featured image in wordpress.

Solving

Help! True / False to include or exclude featured image in wordpress.

  • Hi
    I’ve been trying to add an option to wordpress to show or hide the featured image in posts (not all posts need a featured image). I’ve successfully added the button to wp admin but I’m struggling adding the correct code to functions.php

    if( get_field(‘remove_featured_image’))
    {
    remove_action( ‘genesis_meta’, ‘navigation_full_featured_image’ );
    remove_action( ‘genesis_entry_header’, ‘genesis_do_singular_image’, 1 );
    }

    Any advice would be massively appreciated.

    Thanks loads 🙂

    Will

  • 1) You have to make sure you’re calling remove_action() after the corresponding add_action() is called by using the correct hook and priority. More than likely you’ll need to put this inside another action that is called at the right time. For example on the WP “init” hook.

    2) Make sure that you are using the same priority that is used for the add_action() call.

  • It’s working fine when I put this in functions.php

    remove_action( ‘genesis_meta’, ‘navigation_full_featured_image’ );
    remove_action( ‘genesis_entry_header’, ‘genesis_do_singular_image’, 1 );

    But when I add it in this format it’s not doing anything.

    if( get_field(‘remove_featured_image’))
    {
    remove_action( ‘genesis_meta’, ‘navigation_full_featured_image’ );
    remove_action( ‘genesis_entry_header’, ‘genesis_do_singular_image’, 1 );
    }

  • Because in functions.php, before the loop, ACF does not know what post to get the value from. Missed that earlier.

    You need to supply ACF with the post ID. This needs to happen inside “the loop” or you need to figure out the post ID that will be loaded, if it is a single post.

    for example

    
    $queried_object = get_queried_object();
    if (is_a($queried_object, 'WP_Post')) {
      if (get_field('', $queried_object->ID)) {
        ....
      }
    }
    

    Even using this it may not work until after ‘init’ and you should never call an acf function before ‘acf/init’ the reason for this is that it causes premature initialization of ACF that can cause other issues. I would suggest trying to to this on and the WP ‘init’ action with a high priority.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.