Genius is nothing but great effort applied. – Awa Kenzo

Genesis Code Snippets

Here is my collection of code snippets I use build child themes in Genesis. I’m constantly adding to this list.

If it’s not here, please check out Bill Erickson’s Genesis Quick Tips, Gary’s Code, or Brian Gardner’s Code. Thanks to Gary J for advice on cleaning up the code snippets to adhere with coding standards.

Last update: 2012-04-16

Quickly navigate to a section:

Header

Custom Header Support

/** Add custom header support */
add_theme_support( 'genesis-custom-header', array( 'width' => 960, 'height' => 100, 'textcolor' => '444', 'admin_header_callback' => 'minimum_admin_style' ) );

Remove Header

/** Remove Header */
remove_action( 'genesis_header', 'genesis_do_header' );

Remove Title & Description

/** Remove Title & Description */
remove_action( 'genesis_site_title', 'genesis_seo_site_title' );
remove_action( 'genesis_site_description', 'genesis_seo_site_description' );

Build a Custom Title

remove_action( 'genesis_site_title', 'genesis_seo_site_title' );
add_action( 'genesis_site_title', 'child_seo_site_title' );
/**
 * Remove title, add <span> inbetween Buzz.
 * Then add title back to header.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_seo_site_title() { 

	echo '<h1 id="title"><span>Buzz</span>Montgomery.com</h1>';

}

Move Navigation Menu

/** Move primary nav menu */
remove_action( 'genesis_after_header', 'genesis_do_nav' );
add_action( 'genesis_before_header', 'genesis_do_nav' );

Move Secondary Navigation Menu

/** Move secondary nav menu */
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_before_header', 'genesis_do_subnav' );

Favicon To CDN

add_filter( 'genesis_pre_load_favicon', 'child_favicon_filter' );
/**
 * Change the location of the favicon reference.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_favicon_filter( $favicon_url ) {

	return 'http://cdn.yoursite.com/wp-content/themes/child/images/favicon.ico';

}

Stylesheet To CDN

remove_action( 'genesis_meta', 'genesis_load_stylesheet' );
add_action( 'genesis_meta', 'child_stylesheet_cdn' );
/**
 * Change the location of the stylesheet reference.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_stylesheet_cdn() {

	echo '<link rel="stylesheet" href="http://cdn.yourdomain.com/wp-content/themes/child/style.css" type="text/css" media="screen" />'."\n";

}

Move jQuery To Google CDN

add_action( 'wp_enqueue_scripts', 'script_managment', 99);
/**
 * Change the location of jQuery.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function script_managment() {
	  wp_deregister_script( 'jquery' );
	  wp_deregister_script( 'jquery-ui' );
	  wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' );
	  wp_register_script( 'jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js' );
	  wp_enqueue_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array( 'jquery' ), '4.0', false );
	  wp_enqueue_script( 'jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js', array( 'jquery' ), '1.8.16' );
}

Add viewport for responsive (mobile) design

/** Add Viewport meta tag for mobile browsers */
add_action( 'genesis_meta', 'child_viewport_meta_tag' );
function child_viewport_meta_tag() {
	echo '<meta name="viewport" content="width=device-width, initial-scale=1.0"/>';
}

Template

Place WP-Cycle (must have WP-Cycle plug-in installed first)

/** Place WP-Cycle */
add_action( 'genesis_before_loop', 'wp_cycle' );

Image Sizes

/** Custom image sizes */
add_image_size( 'Slideshow', 520, 280, TRUE );
add_image_size( 'Small Thumbnail', 70, 70, TRUE );

Post Thumbnails

/** Add support for post thumbnails */
set_post_thumbnail_size( 300, 165, TRUE );

Post Formats

/** Add support for post formats */
add_theme_support( 'post-formats', array( 'aside', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video', 'audio' ));

/** Add support for post format images */
add_theme_support( 'genesis-post-format-images' );

Unregister Site Layouts

/** Unregister site layouts */
genesis_unregister_layout( 'sidebar-content' )
genesis_unregister_layout( 'full-width' )
genesis_unregister_layout( 'content-sidebar-sidebar' );
genesis_unregister_layout( 'sidebar-sidebar-content' );
genesis_unregister_layout( 'sidebar-content-sidebar' );

Force Layout

add_filter( 'genesis_pre_get_option_site_layout', 'child_do_layout' );
/**
 * Force layout
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_do_layout( $opt ) {
    $opt = 'full-width-content'; // You can change this to any Genesis layout
    return $opt;
}

Use the Genesis Custom Loop

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'custom_loop' );
/**
 * Remove default loop. Execute Custom Loop Instead.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function custom_loop() {
global $paged;

	 $args = (array(
		'category_name' => 'Blog',
		'order' => 'asc',
		'order_by'=> ‘title’,
	 	'paged' => $paged,
	 	'posts_per_page' => 5
	));

genesis_custom_loop( $args );

}

genesis();

Create a custom homepage using home.php (see Create a Genesis Page Template for more customization)

<?php 

remove_action( 'genesis_loop', 'genesis_do_loop' ); // Remove default loop
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); // Set homepage width to FULL (optional can be removed)
add_action( 'genesis_loop', 'child_home_loop_helper' ); // Execute custom child loop
/**
 * Remove default loop. Execute child loop instead.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_home_loop_helper() { ?>

<!--Custom Homepage Code Here -->

<?php }

genesis();

Use the Genesis Grid Loop in home.php

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function child_grid_loop_helper() {

	if ( function_exists( 'genesis_grid_loop' ) ) {
		remove_action( 'genesis_before_post_content', 'generate_post_image', 5 );
		genesis_grid_loop( array(
			'features' => 2,
			'feature_image_size' => 'large',
			'feature_image_class' => 'aligncenter post-image',
			'feature_content_limit' => 0,
			'grid_image_size' => 'grid',
			'grid_image_class' => 'alignleft post-image',
			'grid_content_limit' => 0,
			'more' => __( 'Continue reading...', 'genesis' ),
			'posts_per_page' => 6,
		) );
	} else {
		genesis_standard_loop();
	}

}

genesis();

Filter out duplicate posts in the WordPress Loop

$do_not_duplicate = array(); 
    query_posts( 'cat=featured&showposts=6' ); while ( have_posts() ) : the_post(); 
    $do_not_duplicate[] = $post->ID;
    <!-- do stuff -->
endwhile; wp_reset_query();

Add Widgets And WP-Cycle To home.php

remove_action( 'genesis_after_post_content', 'genesis_post_meta', 10 );
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_before_loop', 'wp_cycle' );
add_action( 'genesis_loop', 'child_home_loop_helper' );
/**
 * Add Widgets and WP-Cycle to home.php.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_home_loop_helper() { ?>

<div id="homepage_widgets">
	<div class="column1">
		<ul>
 			<?php if ( !function_exists( 'dynamic_sidebar' ) || !dynamic_sidebar( 'Home 1' ) ){ ?><?php } ?>
 		</ul>
	</div>
	<div class="column2">
		<ul>
 			<?php if ( !function_exists( 'dynamic_sidebar' ) || !dynamic_sidebar( 'Home 2' ) ){ ?><?php } ?>
 		</ul>
	</div>
</div>

<?php }

genesis();

Body

Remove Post Titles

/** Custom post titles */
remove_action( 'genesis_post_title','genesis_do_post_title' );

Remove Post Titles Using Conditional Tags

remove_action( 'genesis_post_title','genesis_do_post_title' );
add_action( 'genesis_post_title','child_post_title(' );
/**
 * Remove Post Title with conditional tag.
 *
 */
function child_post_title() {

if ( is_front_page() ) {
	remove_action( 'genesis_post_title','genesis_do_post_title' );

}}

See the WordPress Codex for more conditional tags.

Remove Edit Link

/** Remove Edit Link */
add_filter( 'edit_post_link', '__return_false' );

Remove default Post Image

/** Remove default post image */
remove_action( 'genesis_post_content', 'genesis_do_post_image' );

Add Post Image above Title

/** Add custom post image above post title */
add_action( 'genesis_before_post_content', 'generate_post_image', 5 );
function generate_post_image() {

	if ( is_page() || ! genesis_get_option( 'content_archive_thumbnail' ) )
		return;

	if ( $image = genesis_get_image( array( 'format' => 'url', 'size' => genesis_get_option( 'image_size' ) ) ) ) {
		printf( '<a href="%s" rel="bookmark"><img class="post-image" src="%s" alt="%s" /></a>', get_permalink(), $image, the_title_attribute( 'echo=0' ) );
	}

}

Custom Post Info (with Shortcodes)

add_filter( 'genesis_post_info', 'child_post_info_filter' );
/**
 * Custom Post Info with shortcodes.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_post_info_filter( $post_info ) {
	return '[post_date] by [post_author_posts_link] at [post_time] [post_comments] [post_edit]';
}

Remove Post Info (Author and Date information)

/** Remove post info */
remove_action( 'genesis_before_post_content', 'genesis_post_info' );

Remove Post Meta

/** Remove the post meta function */
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );

Custom Post Meta (With Shortcodes)

add_filter( 'genesis_post_meta', 'child_post_meta_filter' );
/**
 * Custom Post Meta with shortcodes.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_post_meta_filter( $post_meta ) {
	return '[post_categories] Tagged with [post_tags]';
}

Read More Link

add_filter( 'excerpt_more', 'child_read_more_link' );
add_filter( 'get_the_content_more_link', 'child_read_more_link' );
add_filter( 'the_content_more_link', 'child_read_more_link' );
/**
 * Custom Read More link.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_read_more_link() {
	return '<a class="more-link" href="' . get_permalink() . '" rel="nofollow">Continue Reading...</a>';
}

Change Avatar Size

add_filter( 'genesis_comment_list_args', 'child_comment_list_args' );
/**
 * Change Avatar size.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_comment_list_args( $args ) {
	return array( 'type' => 'comment', 'avatar_size' => 42, 'callback' => 'genesis_comment_callback' );
}

Sidebars

Remove Default Sidebar

/** Remove default sidebar */
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );

Remove Secondary Sidebars

/** Remove secondary sidebar */
unregister_sidebar( 'header-right' );
unregister_sidebar( 'sidebar' );
unregister_sidebar( 'sidebar-alt' );

Add Custom Sidebar

/** Add custom sidebar */
genesis_register_sidebar(array(
	'name'=>'Alternative Sidebar',
	'id' => 'sidebar-alternative',
	'description' => 'This is an alternative sidebar',
	'before_widget' => '<div id="%1$s"><div class="widget %2$s">',
	'after_widget'  => "</div></div>\n",
	'before_title'  => '<h4><span>',
	'after_title'   => "</span></h4>\n"
));

Add Widgets

add_action( 'genesis_sidebar', 'child_do_sidebar' );
/**
 * Add a widget/sidebar area.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_do_sidebar() {
	if ( !function_exists( 'dynamic_sidebar' ) || !dynamic_sidebar( 'Sidebar Name' ) ) {
}}

Footer

Custom Footer and Footer Nav (with Shortcodes)

add_filter( 'genesis_footer_output', 'child_output_filter', 10, 3 );
/**
 * Custom Footer with shortcodes.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_output_filter( $backtotop_text, $creds_text ) {

	$backtotop_text = '[footer_backtotop text="Top of Page"]';
	$creds_text = wp_nav_menu( array( 'menu' => 'Footer Nav' ));
	return '<div>' . $backtotop_text . '</div>' . '<div>' . $creds_text . '</div>';

}

Remove Footer Widgets (Genesis 1.6+)

/** Remove footer widgets */
remove_theme_support( 'genesis-footer-widgets', 3 );

Remove Footer

/** Remove Footer */
remove_action( 'genesis_footer', 'genesis_do_footer' );

Create a Custom Footer with HTML

remove_action( 'genesis_footer', 'genesis_do_footer' );
add_action( 'genesis_footer', 'child_footer_html' );
/**
 * Custom Footer with HTML.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_footer_html() { ?>

	<div class="banner alignleft">
		<!-- openx text/code here -->
	</div>
	<div class="creds">
		<!-- credit text/code here -->
	<div>	

<?php }

Admin

Get settings from database

genesis_get_option( 'option_name', $this->settings_field )

Get settings from database for custom metaboxes

genesis_get_custom_field( 'meta_box_option_name' )

Development

Cache Bust your CSS

add_filter( 'stylesheet_uri', 'child_stylesheet_uri' );
/**
 * Cache bust the style.css reference.
 *
 * @author Greg Rickaby
 * @since 1.0.0
 */
function child_stylesheet_uri( $stylesheet_uri ) {
    return add_query_arg( 'v', filemtime( get_stylesheet_directory() . '/style.css' ), $stylesheet_uri );
}

Greg Rickaby runs on the Genesis Framework

Genesis Framework

The theme you're viewing is the eleven40 Child Theme, which was built on Genesis.

Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!

Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.

Comments

  1. 1

    Thanks for your posting ! That ‘s so great. I am trying with Genesis and hope you will have more great post like that !

    Regard,

  2. 2
    Vivienne :

    Hi, thanks for a great post. I’m using the genesis_unregister_layout() option to limit the layouts that my users can see or select. The method call “genesis_unregister_layout(‘full-width’);” didn’t work for me though, I had to change that into “genesis_unregister_layout(‘full-width-content’);”. Hope this helps someone.
    Kind regards,
    Vivienne

  3. 3

    Thanks for putting this together. It was very helpful, especially as someone proficient in WordPress, but just learning Genesis.

    Anyway the only thing this page didn’t answer for me was how to remove the “top of page” link. I just wanted to share that in case anyone finds this page doing the same search I did:

    To remove the “top of page” link in Genesis:
    Add “display: none;” to the #footer .gototop selector

  4. 5
    joe :

    Hi Greg,

    I just started delving into the WordPress game, so if I sound like a greenhorn, it’s because I am :). I would like to remove the footer widgets from the Outreach genesis theme. I cannot figure out where to put the code you listed above… in the Theme Functions php, in the Footer-Widgeted php? Do I replace any existing text in those files, or just add it somewhere specific?

    Also, how do I replace the “Outreach Church Theme” with my own logo?

    Any help is appreciated, thanks!

    -joe

    • 6

      Morning Joe,

      Open up functions.php and scroll down to line 10 through 14:

      // Add widgeted footer section
      add_action('genesis_before_footer', 'outreach_include_footer_widgets'); 
      function outreach_include_footer_widgets() {
          require(CHILD_DIR.'/footer-widgeted.php');
      }

      Delete those lines of code!

  5. 8
    Jason :

    I was able to remove the default footer information from the outreach them. I now have a blank box for the footer. How would I go about adding my own logo to the bottom left of the empty footer?

  6. 9

    Hi Greg, how did you put that 125ads in the sidebar?
    They look awesome.
    Thank you

    • 10

      It’s just a widget with HTML code! All the images float: left;

      <a href="http://gregrickaby.com/go/genesis-theme" target="_blank"  rel="nofollow"><img src="http://cdn.gregrickaby.com/affiliate/genesis_125x125.png" alt="Genesis Framework for WordPress" border="0" /></a>
      
      • 11

        Ok, so you just put a normal HTML code into a widget.
        and what how did you make them look awesome? I mean, how did you apply that float:left; ?
        Thank you

      • 12

        I’m playing with the sample child theme as you are using, and I use the whole width for the sidebar which is 300px by setting the padding to 5px 0 5px 0;
        .widget-area .textwidget {
        padding: 5px 0 5px 0;
        }

        Putting the normal html as you showed me giving me those 125ads in the left which I want them in the middle instead :D

  7. 13

    Good stuff Greg.
    I like your snippets on moving overhead to a CDN.
    At what point should a person move their header calls to a CDN?
    Would you wait until their is a speed issue or just do it automatically?
    I’m hosted on Synthesis so speed isn’t a problem, yet:-).

  8. 15

    I’m setting up biographies to display in a lightbox, so I need a custom page template for Genesis that basically has only the content for the page in divs created only for that page and so the body background does not display. What do you recommend?

  9. 16

    Thank Greg for sharing all this ,I notice you switched to “jQuery To Google CDN” , I followed your instructions few days ago and moved jQuery to Amazon CDN , any reason to switch to google CDN ?

    beside that
    /** Place WP-Cycle */
    add_action( ‘genesis_before_loop’, ‘wp_cycle’ );
    gives me error when I put in genesis simple hook , any idea why ?
    thanks

    • 17

      Most folks don’t have access to Amazon Cloudfront ($$$)… so I switched the code to Google, since Google hosts those scripts for free.

      To answer your second question with a question, do you have the WP-Cycle plug-in installed?

  10. 19
    SteveC :

    Greg,

    What’s the “right” way to remove the date from comments? I removed the date from posts easily enough with Simple Edits, but I also want to remove date/time from the post comments as well. Thanks for all the great information here.

  11. 21

    I am new to this and I cannot figure out how to change the padding in my sidebar. My recent post titles are on the edge almost about to cut off on the right side.

  12. 22

    Thanks or the code snippets. They are very helpful. I’m new to Genesis and am using the eleven40 child them just like this site. I want to move the primary navigation above the header and have it styled like this page. I’m able to use your code snippet to move the navigation, but what’s the trick to styling it so that it floats to the right, etc, etc? Is there something simple I’m missing or do I just need to do it by adding styles to the style.php?

  13. 23

    This is one of the simplest, yet most useful Genesis resources out there. Thank you so much for sharing it. I’ve been using your page for a long time and it just saved me once more with the customization for the site title… and again, you nailed it.

    Thank you!

  14. 25
    Scott :

    Hello. Thank you for sharing all of this. I’m fairly new to PHP and WordPress, and completely new to Genesis, and so I’m struggling to figure out how to do something that seems simple: I want to add a class to #content and #sidebar. Basically, I want to run an equal height column script, and I need to apply the same class to them. How can I accomplish this without altering the core files?

    Thanks,
    Scott

  15. 26

    Greg…Great post! Just got the Genesis framework. I found your site and hope you can give me some help with a tiny bit of code.

    I have inserted a “hook before content” so my 468×60 banner ad can show throughout my website. Everything works fine! My question: Is there a way for this hook not to show this particular 468×60 ad on my home page but continue to show ads throughout my site? I don’t know how to edit the HOOK so this can happen. All I got was a link to wordpress conditionals tags page. This is the hook code below. Any help is appreciated!

    genesis_register_sidebar( array(
    ‘id’ => ‘before-posts-sidebar’,
    ‘name’ => ‘Before Posts’,
    ‘description’ => ‘This is a sidebar that goes before the posts in the #content.’,
    ) );
    /** add sidebar before post end **/

    add_action( ‘genesis_before_loop’, ‘child_before_posts_sidebar’ );
    /** Loads a new sidebar before the posts in the #content */
    function child_before_posts_sidebar() {

    echo ”;
    dynamic_sidebar( ‘before-posts-sidebar’ );
    echo ”;

    }

  16. 27

    Hello. Thanks so much for this post. I have downloaded the Simple Hooks Plugin. However, I still cannot figure out what code would remove the post date and who the post is written by. This appears at the beginning of each post.
    Thanks very much.
    Susanna

  17. 29

    Hi, great post!!

    I want to remove dates on a post and I added the codes above in function.php but it doesn’t work. Could you help me to do that ?? I use Serenity genesis child theme.

    Any advice would be very appreciate.

    Thanks. :)

  18. 32
    Andrei :

    Thanks for the great article! I’m trying to display the featured image below the post title and above the post text. Is there a way to do that?

  19. 34

    I created a custom landing page, which I only want people to see the first time they come to my site. Now the problem is that when you click on the header, it takes you back to the landing page instead of the home page. How can I change the link in the header?

  20. 36

    Ultimate resource for genesis users. Thanks Greg :)

  21. 37

    Hi Greg, I want to replace the older/newer blog page navigation text links with images, I think I would use the genesis_after_endwhile Hook, but I am at a loss as to what code should go in there. Could you help?

  22. 38

    Hi Greg,

    Just a note to say thanks for posting all of this. This page helped me through my learning phase for the genesis framework.

    The time you took to post these items is appreciated!

    Thanks!

Trackbacks

  1. [...] source for code snippets http://gregrickaby.com/2011/02/genesis-code-snippets.html This entry was posted in Customizations, Genesis Tips, PHP. Bookmark the permalink. ← [...]

Leave a Comment

Comment Policy: Thanks for visiting my site! Comments are a permanent record of who you are and what you stand for. Your words are your own, so be nice and helpful if you can. Unhelpful, disrespectful, or spammy comments will be deleted. All comments are read and appreciated, and if you have a question, I will try to respond within a couple days.

*