Search code examples
phpwordpressgenesis

How to add a widget before default feed on the home page


In my Genesis Magazine pro theme I'm trying to add a widget area between the header and the content. What I'm trying to do is perfectly described here: https://wpsites.net/web-design/add-widget-to-magazine-pro-front-page/ but it doesn't seem to work. Nothing shows up when I add a new widget into the new before-home-top widget.

I've tried replacing my front-page.php with

<?php
/**
 * Magazine Pro.
 *
 * This file adds the front page to the Magazine Pro Theme.
 *
 * @package Magazine
 * @author  StudioPress
 * @license GPL-2.0+
 * @link    http://my.studiopress.com/themes/magazine/
 */

add_action( 'genesis_meta', 'magazine_home_genesis_meta' );
/**
 * Add widget support for homepage. If no widgets active, display the default loop.
 *
 * @since 3.0.0
 */
function magazine_home_genesis_meta() {

    if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {

        // Force content-sidebar layout setting.
        add_filter( 'genesis_site_layout', '__genesis_return_content_sidebar' );

        // Add magazine-home body class.
        add_filter( 'body_class', 'magazine_body_class' );

            // Add homepage widgets.
        add_action( 'genesis_before_content_sidebar_wrap', 'before_home_top' );

        // Remove the default Genesis loop.
        remove_action( 'genesis_loop', 'genesis_do_loop' );

        // Add homepage widgets.
        add_action( 'genesis_loop', 'magazine_homepage_widgets' );

    }

}

// Add body class to front page.
function magazine_body_class( $classes ) {

    $classes[] = 'magazine-home';

    return $classes;

}


function before_home_top() {

    genesis_widget_area( 'before-home-top', array(
        'before' => '<div class="before-home-top widget-area">',
        'after'  => '</div>',
    ) );

}

// Output the widget areas for the front page.
function magazine_homepage_widgets() {

    echo '<h2 class="screen-reader-text">' . __( 'Main Content', 'magazine-pro' ) . '</h2>';

    genesis_widget_area( 'home-top', array(
        'before' => '<div class="home-top widget-area">',
        'after'  => '</div>',
    ) );

    genesis_widget_area( 'home-middle', array(
        'before' => '<div class="home-middle widget-area">',
        'after'  => '</div>',
    ) );

    genesis_widget_area( 'home-bottom', array(
        'before' => '<div class="home-bottom widget-area">',
        'after'  => '</div>',
    ) );

}


genesis();

and I added the following to my functions.php

genesis_register_sidebar( array(
    'id'          => 'before-home-top',
    'name'        => __( 'Before Home Top', 'magazine-pro' ),
) );

I'm looking to add a custom HTML widget in there and nothing is showing up. Any help would be much appreciated!


Solution

  • The name of the sidebar you registered in functions.php is before-home-top.

    There's a conditional in your front page code that is checking to see if any of three sidebars are active (contain widgets). The reason you're not seeing anything is because none of those checks are for before-home-top.

    Your problem is this line:

    if ( is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
    

    Add your sidebar to that list of checks:

    if ( is_active_sidebar( 'before-home-top' ) || is_active_sidebar( 'home-top' ) || is_active_sidebar( 'home-middle' ) || is_active_sidebar( 'home-bottom' ) ) {
    

    You may also wish to remove any sidebars that you don't intend to register.