There’s a lot of small code snippets I use often but don’t warrant their own post. This is where I’ll collect them. If you have any quick tips, feel free to share in the comments.
Also check my general code snippets page.
Setup the child theme
As described here, it’s a good idea to create a theme setup function in which you’ll place all the filters, actions, and theme-supported features. This is what I include at the top of my functions.php file in my child themes. Any time below you see an add_action or add_filter, that part goes in the setup function, and the function itself goes after the setup function.
Force a page layout
This is very useful for ensuring custom pages you build for a client aren’t broken by them changing the page layout (ex: home page). Or, changing the page layout to something other than the default on archive pages (ex: category).
Unregister unused page layouts
Structural Wrap
This adds a div with the class of “wrap” in an element. In the code below I’m adding it to #inner for a full-width page layout. More details.
Add Image Sizes
See Mark Jaquith’s postfor details. This adds an image size named ‘feature’ with a fixed size of 600×250.
add_image_size('feature', 600, 250, true);
Remove Post Info
remove_action('genesis_before_post_content', 'genesis_post_info');
Modify Post Info
Shortcode Reference
Remove Post Meta
remove_action('genesis_after_post_content', 'genesis_post_meta');
Modify Post Meta
Change Excerpt More text […]
Remove Footer
remove_action('genesis_footer','genesis_do_footer');
Remove Footer and Footer Markup
(thanks Paul de Wouters)
Customize the Search Form text
Customize the Search Button text
Remove Breadcrumbs
remove_action('genesis_before_loop', 'genesis_do_breadcrumbs');
Remove Home from Breadcrumbs
add_filter('genesis_home_crumb', '__return_false');
Customize the Breadcrumb
Full list of arguments here.
Customize the Site Title (in #header)
This is useful if you want to use the default site title (Settings > Title) but style different elements of it differently. This specific code searches for “of” in the site title, and changes it to <em>of</em>.
Remove the Post Title
remove_action('genesis_post_title','genesis_do_post_title');
Display Description of Menu Items
To add a description to a menu item, go to Appearances > Menus. At the top right click “Screen Options”, then check “Description”. Now you can click the dropdown arrow next to menu items and add a description. The below code will make it visible on the site. Here’s another, more detailed approach.
Register a Sidebar
Unregister a Sidebar
unregister_sidebar('sidebar-alt');
Customize Read More Link
CSS – Images scale to content area (useful for Responsive Design)
Nur Ahmad Furlong says
Thanks for this very helpful list of snippets like all your others. It’s a constant struggle when you’re not able to really dig deep into genesis to know how to target certain elements and even what they’re called to begin with so having snippets by guys like you make all the difference.
I’ve been scouting around trying to find a way of inserting genesis breadcrumbs into a very customized single CPT template. Because the template is so highly customised it uses very little actual genesis code but now I realise the need to try to do any custom stuff in a very genesis way.
Does anyone know if there are ways of inserting genesis template parts manually into non-genesis templates?
My theme is mostly genesis but one or 2 of the custom page templates are more pure wp based.
Additionally any help in tracking down tuts which cover custom post type “single” templates would be great, so far the majority of advanced tuts tackle archives.
Bill Erickson says
Every page template in a Genesis child theme should end with genesis(); No matter how much custom code you’re adding to it, you should still be using Genesis.
Here’s a landing page template that Jared and I use as a base when creating landing pages ( https://gist.github.com/billerickson/ff8d02cfeec041b3af6b ). It removes pretty much all the features of Genesis. Comment out the parts you want to keep.
Here’s an example of how you’d unhook the main content area and replace it with your own ( https://gist.github.com/billerickson/1ae22572f2a8c56cb4aa ).
Mario says
How can i change [post_categories] variable?
I’d like to have “Published on” [post_date] in [post_categories] where [post_categories] = categories and not “Filed under:” + categories.
I’d like also to filter info and meta to apply only on homepage or singlepost/archies, not globally.
Bill Erickson says
Use [post_date before="Published on "] [post_categories before="in "].
In your post info or post meta filter, you can use conditionals.
if( is_home() ) $post_info = '';
Chris Field says
Hi Mr. Erickson,
Could you provide a little background on your use of “genesis_setup” with “child_theme_setup” (first snippet on this page) as opposed to using “include_once( get_template_directory()” to launch the Genesis init.php file? As in:
https://github.com/gregrickaby/genesis-sample/blob/sass/functions.php
Your method appears to better follow the WordPress standard, however the latter method is what Genesis uses in its child themes.
I have not been able to discern much of a difference between the two approaches, aside from maybe conforming to that WordPress standard.
Would be very grateful for your thoughts on the two approaches!
Bill Erickson says
Funny you should ask, I just wrote this as a comment on GitHub a few weeks ago: https://github.com/cdils/in-the-beginning/issues/5#issuecomment-49202594
Exelexys says
Given the code provided in Genesis child theme function.php files (e.g., link in Chris Field post above), and your ‘Setup the child theme’ snippet, what would code added to Genesis child theme functions.php by developers look like?
Many thanks
Bill Erickson says
If you are using a theme setup function, you would but all your add_action, add_filter, remove_action and remove_filter’s inside the setup function, and then place the actual functions after it. Like this: https://github.com/billerickson/BE-Genesis-Child/blob/master/functions.php
If you are not using a theme setup function (you are doing the include_once() at the top), you can just drop the function and its add_action, add_filter… anywhere in the functions.php file. See all the StudioPress child themes as an example
Scott says
Bill:
I’m trying to change the title to the search results page on a child theme, but the script is in genesis search.php
I would like the search title to be “Listing Results” and perhaps include the query that was made i.e. “Listing Results for 3 bdrm 2 bath”
I’ve attempted to un-register and re-register the function as a child to no avail:
remove_action( ‘genesis_do_search_title’ );
add_action( ‘child_genesis_do_search_title’ );
function child_genesis_do_search_title() {
$title = sprintf( ‘%s %s’, apply_filters( ‘genesis_search_title_text’, __( ‘Listing Results:’, ‘genesis’ ) ), get_search_query() );
echo apply_filters( ‘genesis_search_title_output’, $title ) . “\n”;
}
HELP!
Thank you
Bill Erickson says
My guess is you’re unregistering the function in functions.php, which is too early. There’s two ways you can do this:
1. In functions.php, write a function that filters ‘genesis_search_title_text’. Ex: https://gist.github.com/billerickson/32bf346b5e17d2d5ed47
2. Create your own search.php file in your child theme, which will then be used instead of Genesis’ search.php. The search results will no longer have a title, so you will create a function in your search.php file to add the title you want.
For your specific needs, #1 is best. But if you plan to make lots of customizations to the search results page, go with #2.
Paul says
Hi Bill.
Common story here, I’ve been searching Google for hours looking for an answer, but have come up empty. I’m trying to re-position the ‘Search Results for:’ heading on the search results page. I know I need something like this…
remove_action( ‘genesis_before_loop’, ‘XXX_XXX_XXX_XXX_XXX’, 15 );
add_action( ‘genesis_before_content’, ‘XXX_XXX_XXX_XXX_XXX’, 15 );
but for the life of me, I can’t figure out what to put where the X’s are; I’ve tried every combination of ‘genesis’ ‘search’ ‘description’ ‘title’ etc that I can think of.
I’m using ‘WordPress 4.4’ and the Genesis child them ‘Focus Pro.’
Any help would be much appreciated!
(Thanx for this page, I now know what the ’15’ after the X’s means!)
Thanx Bill!
Paul
Bill Erickson says
The code that powers that is in genesis/search.php: https://gist.github.com/billerickson/40f409bde3eb7370091e
Create your own search.php file in your child theme. This will then be used instead of the Genesis one (WordPress first looks in the child theme for a specific file, then the parent theme). You can then place whatever function you like and attach it to whatever hook you like. Since the code is in search.php, it will only apply to search results.
Paul says
Thank Bill! That worked perfectly!
That was EXACTLY what I was looking for!
Much, much, much appreciated!
Paul 🙂
Paul Anderson says
Hi Bill, Paul again.
A many moons ago, I found this code of yours (which is actually what lead me here): https://www.billerickson.net/default-category-and-tag-titles
I was wondering if there was a way to also display the NUMBER of posts in the category, so that if I went to the ‘toys’ category page it would display at the top of the page something like…
There are 10 posts in category: toys
I did quite an extensive search but wasn’t able to find much in the way of a solution for this. To be honest, I’m surprised this isn’t standard on WP or that StudioPress didn’t build it into Genesis — showing the number of posts in a category gives a person a good idea of how many posts they may have to wade through to find what they’re looking for.
The closet thing I found was this:
$wp_query->found_posts
which does display the number of posts, but it appears up in the top left corner.
Paul 🙂
Bill Erickson says
Yes, $wp_query->found_posts is what you’re looking for. But remember it must be returned, not echo’ed. My guess is you are echo-ing it which is why it displays in the top left corner. Example: https://gist.github.com/billerickson/cd8413a564c50cb60bfd
Paul Anderson says
Hi Bill, thank-you very much for the code! And, yes, you are correct, I was echo-ing it!
Paul 🙂
Paul Anderson says
P.S. You need a ‘donate’ button on this page – it has helped me immensely!
Bill Erickson says
Added to the top of the post 🙂
Darren says
Bill,
Since I do not want to use genesis_archives, HOW can I get rid of them in the theme customizer and also on the Theme Settings page?
I tried using the normal way as such: https://gist.github.com/billerickson/1773c78088186fc1b4a928c35230f0cf
Thanks,
Darren
Bill Erickson says
I never use the customizer so I’m not sure. Hopefully someone else can respond with the answer, or you can try StudioPress support.
Johnny C says
Bill i know you are a busy man, if you have time, i have an issue you might can lead me in the right direction. I have added a walker function for the genesis menu.
For some reason on genesis 2.2.7 it only displays the description above the title and the version before this it worked correct! I am assuming that some code changed in the genesis framework that does not work correct with this function.
Here is the code i am using for the description!
https://gist.github.com/billerickson/f1de0f5954ed64c69ef04e88fcd251c3
Any insight on this would be great, i figure you would know better than anyone! Thanks John
Bill Erickson says
Try the method described here: https://www.billerickson.net/code/add-description-to-wordpress-menu-items/
Johnny C says
Hey Bill thanks for replying so fast! I followed the link you posted prior and was unable to get it to work correctly. I posted on FB and a friend posted some code that he said came from you, well it worked.
function be_add_description( $item_output, $item ) {
$description = $item->post_content;
if (‘ ‘ !== $description )
return preg_replace( ‘/([^<]*?)</', '$1' . '’ . $description . ‘<', $item_output);
else
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'be_add_description', 10, 2 );
I was able to display the description below the titles. Now I am facing the issue of styling, i want to be able to style the title and description alike. The issue is the description is not linked. I assume I would have to change the function to be able to do this. I tried using a class and link and saving in the description area in the menu, all it did was strip it out! Any insight would be awesome. Once again you rock Bill.. thanks JOhnny C
Johnny C says
Hey Bill i got the style figured out.. ” li:hover a, li:hover .menu-description” Thanks man, keep up the great work!
Thanks so much