• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Flexible Footer Widgets in Genesis Framework

Last Updated on May 28, 2018 Favorited: 0 times

I have recently posted a quick hack guide to register different column footer widgets in Genesis and it will work with any of child theme. However, sometimes we need to change the layouts and adding different CSS everytime is time-consuming.

I believe sometimes we need to write CSS manually to change the layout width of a column according to media queries but flexible widgets are helpful in some cases and it is not difficult to implement in a specific section.

Flexible widgets automatically add a specific CSS class based on the number of contents added in that particular widget section.

We are going to use the same method of register and display flexible widgets in Genesis but just changing the hook position. Let’s see how we can make a flexible widget section work for footer Widget.

  1. Setup Widget count Script and add CSS classes according to the number of widgets added in the flexible section.
  2. Adding CSS in the stylesheet that is added by above functions.
  3. Register a widget area aka flexible section with a unique id.
  4. Add the positioning and function to output the flexible widget section, we have registered in (2) with additional flexible-widgets class and id (2) to make it work as (1)

Step 1

Add the following in your child theme’s functions.php

/**
 * Setup widget counts.
 *
 * @param string $id The widget area ID.
 * @return int Number of widgets in the widget area.
 */
function custom_count_widgets( $id ) {
    global $sidebars_widgets;

    if ( isset( $sidebars_widgets[ $id ] ) ) {
        return count( $sidebars_widgets[ $id ] );
    }
}

/**
 * Set the widget class for flexible widgets.
 *
 * @param string $id The widget area ID.
 * @return Name of column class.
 */
function custom_widget_area_class( $id ) {
    $count = custom_count_widgets( $id );

    $class = '';

	if ( $count == 1 ) {
		$class .= ' widget-full';
	} elseif ( $count % 3 == 0 ) {
		$class .= ' widget-thirds';
	} elseif ( $count % 4 == 0 ) {
		$class .= ' widget-fourths';
	} elseif ( $count % 2 == 1 ) {
		$class .= ' widget-halves uneven';
	} else {
		$class .= ' widget-halves';
	}

    return $class;
}

Step 2

Add the following in your child theme’s style.css

/* Flexible Widgets
--------------------------------------------- */

.flexible-widgets .wrap {
	max-width: 100%;
	clear: both;
	margin: 0 auto;
	padding: 60px 30px 0;
}

.flexible-widgets.widget-area .widget {
	float: left;
	padding-left: 20px;
	padding-right: 20px;
	margin-bottom: 40px;
}

.flexible-widgets.widget-halves .widget:nth-child(odd),
.flexible-widgets.widget-thirds .widget:nth-child(3n+1),
.flexible-widgets.widget-fourths .widget:nth-child(4n+1) {
	clear: left;
}

@media only screen and (max-width: 767px) {
	.flexible-widgets.widget-fourths .widget,
	.flexible-widgets.widget-halves .widget,
	.flexible-widgets.widget-thirds .widget {
		float: none;
		width: 100% !important;
	}
	.flexible-widgets .widget {
		padding-left: 0;
		padding-right: 0;
	}
}

@media only screen and (max-width: 959px) {
	.flexible-widgets.widget-fourths .widget {
		width: 50%;
		float: left;
	}
}

@media only screen and (min-width: 960px) {
	.flexible-widgets.widget-full .widget,
	.flexible-widgets.widget-halves.uneven .widget:last-of-type {
		float: none;
		width: 100%;
	}
	.flexible-widgets.widget-fourths .widget {
		width: 25%;
	}
	.flexible-widgets.widget-halves .widget {
		width: 50%;
	}
	.flexible-widgets.widget-thirds .widget {
		width: 33.33%;
	}
}

Step 3

We are registering a widget area with id footer-flexible-widget Add this in child theme’s functions.php

// Register footer flexible widget area
genesis_register_widget_area(
    array(
        'id'          => 'footer-flexible-widget',
        'name'        => __( 'Flexible Widget for Footer', 'text-domain' ),
        'description' => __( 'This is the footer flexible widget section', 'text-domain' ),
    )
);

Step 4

Hook and output the widget before footer (genesis_before_footer) we have just registered. Add this in child theme’s functions.php

add_action( 'genesis_before_footer', 'custom_footer_flexible_widget' );
/**
 * Display Footer Flexible widget area before footer
 */
function custom_footer_flexible_widget() {

    genesis_widget_area( 'footer-flexible-widget', array(
        'before' => '<div class="flexible-widgets widget-area' . custom_widget_area_class( 'footer-flexible-widget' ) . '"><div class="wrap">',
        'after'  => '</div></div>',
	) );
	
}

 

Go to Appearance >> Widgets and populate the widget section that has been made flexible above with 1 or more widgets.

Tweaks.

What if you want flexible widget not to show on front-page but show on other pages? We will use a conditional tag to exclude the widget from showing on the frontpage.

add_action( 'genesis_before_footer', 'custom_footer_flexible_widget' );
/**
 * Display Footer Flexible widget area before footer
 */
function custom_footer_flexible_widget() {
	// if we are on the front page, abort this function.
	if ( ( is_front_page() ) ) {
		return;
	}
    genesis_widget_area( 'footer-flexible-widget', array(
        'before' => '<div class="flexible-widgets widget-area' . custom_widget_area_class( 'footer-flexible-widget' ) . '"><div class="wrap">',
        'after'  => '</div></div>',
	) );
	
}

Please note:- We are using return early hook that’s why there is no ! for like excluding me.

Related Posts

  • Change 3 columns footer widgets in 4 columns widgets in Genesis
  • Replace site footer widget area conditionally in Genesis
  • Flexible Widgetized Front page in Genesis Sample
  • How to set up Flexible Widgets in any Genesis child theme
  • Footer Widgets in Genesis

Categories: Free Content, Genesis Tutorials Tags: flexible widgets, Footer Widgets

Reader Interactions

Comments

  1. PuGaGo says

    September 26, 2019 at 1:21 am

    Hello…

    in default setting genesis, there are 6 widget on footer area

    i just want to use 3 widget, i have use this code :

    add_theme_support( 'genesis-footer-widgets', 3 );

    the result, widget succesed to be 3 widget, but still on 6 part…

    sorry, my english was so bad…

    you can see in my site https://pugago.co.id

    i have add in css,

    .footer-widgets-1,
    .footer-widgets-2,
    .footer-widgets-3 {
    	width: 33%;
    	padding: 1%;
    }
    
    .footer-widgets-1,
    .footer-widgets-2 {
    	float: left;
    }
    
    .footer-widgets-3 {
    	float: right;
    }
    

    but, there still 6 widget part, with 3 showing…

    so, my site footer just showing a half..

    why like that?

    Thanks for your answer before

    • Aryan Raj says

      September 26, 2019 at 2:41 am

      Hi, Your site is in Maintenance Mode. Connect me on chat and send me the details so i look into this.

      Thank you.

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service