Home Forums General Issues Custom sidebar for both pages and posts


Custom sidebar for both pages and posts

  • I’m trying to create an ACF option that will allow me to select a custom sidebar for any given page, category, or post within a category type. This is my code. It works for pages, but not for posts or categories.

    if( is_page() ) {
        $sidebar = get_field('sidebar');
    } elseif( is_category() || is_single() ) {
        $queried_object = get_queried_object();
        $taxonomy = $queried_object->taxonomy;
        $term_id = $queried_object->term_id; 
        $sidebar = the_field('sidebar', $taxonomy . '_' . $term_id);
    if( isset($sidebar) && $sidebar != "" ) {
        if( $sidebar != 'none' ) {
            dynamic_sidebar( $sidebar );
    } else {
        dynamic_sidebar( 'sb-default' );

    The category edit page shows the field option. But I can’t seem to figure out how to make it work.

  • I edited the code in my question above.

    I changed:

    elseif( is_category() )


    elseif( is_category() || is_single() )

    And so now it works for both pages and posts within a certain category, but not the categories themselves (the index/archive/category pages).

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

The topic ‘Custom sidebar for both pages and posts’ is closed to new replies.