Support

Account

Home Forums General Issues Single Post Checkbox Effecting Archives

Solving

Single Post Checkbox Effecting Archives

  • I’m using this location setting to add a checkbox to every Edit screen.

    acf-location

    This is the code i’m using to remove a default function which adds widgets if the ACF checkbox is checked.

    add_action( 'loop_start', 'conditional_widgets' );
    function conditional_widgets() {
    
        $checked = get_post_meta( get_the_ID(), '_custom_field_key', true );
    	
    	if ( $checked ) {
            remove_action( $tag, $function_to_add );
        }
    }

    When i check the box on a single post, the remove_action works but it also executes on all archive page types even though the box is unchecked on these archive edit screens.

    Maybe i have the location rules wrong?

    Do i need to add a conditional tag for each page type or is there another solution?

  • I cannot tell by your code what you’re trying to accomplish and the explanation does not help.

    This line makes no sense to me $checked = get_post_meta( get_the_ID(), '_custom_field_key', true );

  • This is for the checkbox ( true/false ). I could also use something like this :

    $checked = get_field( '_custom_field_key' );

    All i want to do is add the checkbox to all edit screens for single posts/pages and all archive type pages to show the default widgets or when checked, the custom widgets.

    Seems like get_field only works on single posts/pages and NOT on archive page types?

    acf

  • You need to tell ACF where to get the value from. When you’re displaying a post it uses the current post ID. Anything other than this requires the you specify the post ID. This page explains what the value need to be https://www.advancedcustomfields.com/resources/get_field/

  • I thought that might be built into ACF when selecting a location setting which adds the input field on all post/page types including archive archive and single but looks like i need to write the code to cover all types.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.