• Resolved John Dimick

    (@johndimick)


    Is it possible to add a custom sidebar to my homepage (posts page)?

    I can see how to add a custom sidebar to any other page, but I don’t see how to configure the homepage for a custom sidebar.

    I want a “Recent Posts” widget at the top of the default sidebar, but I don’t want one at the top of the homepage sidebar, because the homepage already consists of recent posts.

    The only workaround I can see is to remove the “Recent Posts” widget from the default sidebar, then use the default sidebar only for the homepage. That means I would have to specify a custom sidebar for every other page and post, which seems like a lot of work.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Amit Singh

    (@apprimit)

    Hello,

    Try to add the below code to the functions.php file of the child theme to have a custom sidebar for the home page if it is a post page. Add your sidebar id in the code –

    function prefix_custom_sidebar( $sidebar = 'sidebar' ) {
        if ( is_home() ) {
            $sidebar = 'your-sidebar-id';
        }
        return $sidebar;
    }
    add_filter( 'ocean_get_sidebar', 'prefix_custom_sidebar' );
    Thread Starter John Dimick

    (@johndimick)

    Thank you for a timely reply, Amit.

    I added your code to functions.php in the child theme.

    In the code, I changed ‘your-sidebar-id’ to ‘sidebar-test’.

    The result was an additional sidebar — “sidebar test” — added to the list of available sidebars. Nothing happened on the homepage, which still shows the default sidebar. I see no way to assign a different sidebar to the homepage.

    -John

    Plugin Author Amit Singh

    (@apprimit)

    The above code should work. Have you added any widget in your custom sidebar? Have you added the correct sidebar id? You will see the sidebar id under Theme Panel > Sidebar page.

    Thread Starter John Dimick

    (@johndimick)

    “Have you added any widget in your custom sidebar?”

    Yes, I have added a widget to the “sidebar-test” custom sidebar:

    https://www.guitarist.com/wp-content/uploads/2019/06/sidebar-widget.png

    “Have you added the correct sidebar id?”

    Do you mean have I added the correct sidebar ID to the functions.php in the child theme? I think so, since it appears under Theme Panel > Sidebar. Here is the code in functions.php:

    <?php
    
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
    function my_theme_enqueue_styles() {
     
        $parent_style = 'parent-style'; // OceanWP style
     
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style ),
            wp_get_theme()->get('Version')
        );
    }
    
    #Attempt at homepage custom sidebar
    
    function prefix_custom_sidebar( $sidebar = 'sidebar' ) {
        if ( is_home() ) {
            $sidebar = 'sidebar-test';
        }
        return $sidebar;
    }
    add_filter( 'ocean_get_sidebar', 'prefix_custom_sidebar' );
    
    ?>

    Is there someplace else to add the correct sidebar ID?

    • This reply was modified 5 years, 5 months ago by John Dimick.
    Thread Starter John Dimick

    (@johndimick)

    Solved. I see now that I didn’t have the sidebar ID correct in the functions.php file. I had omitted the “ocs” in “ocs-sidebar-test”. It works fine now.

    Thanks again, Amit.

    Plugin Author Amit Singh

    (@apprimit)

    Glad to hear that it is fixed!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add Custom Sidebar to Posts Page?’ is closed to new replies.