We have recently learned to create a custom sidebar in Genesis but again this post? Why?
This post is written to give you a quick recap of the old post and guide to make some additional tweaks to your custom post type.
We will also learn to use Custom sidebar on different post pages based on conditional tags similar to showing widgets conditionally in Genesis.
How to Create Custom sidebar in Genesis?
You first need to register a post type. Use Plugin like Pods or ACF to register CPT without going technical.
There are two simple methods to create a custom sidebar
1. Using Plugins
The simplest method to register multiple custom sidebars. I love and use the following plugins to create and manage multiple custom sidebar on clients site
2. The Snippet method
The snippet method is simple. Be sure to follow this.
- Rename the conditional tag is_singular( ‘book’ ) here book will be replaced with your custom post type you have already registered.
- id and function name should be same.
- Make sure to check everything once again.
Here is the Sample snippet. Use the recommended method.
// Step 1.Register new sidebar for CPT book genesis_register_sidebar( array( 'id' => 'book-sidebar', 'name' => 'Book Sidebar', 'description' => 'This is the sidebar for custom post type book.', ) ); // Step 2. Remove the primary sidebar and Add the custom one "book-sidebar" add_action('get_header','wpize_change_genesis_sidebar'); function wpize_change_genesis_sidebar() { if ( is_singular( 'book' ) ) { // Check if we're on a single post for my CPT called "book" remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); //remove the default genesis sidebar //remove_action( 'other action', 'other do action' ); //remove adiditonal features add_action( 'genesis_sidebar', 'wpize_do_sidebar' ); //add an action hook to call the function for my custom sidebar } } //Step 3. Function to output my custom sidebar function wpize_do_sidebar() { dynamic_sidebar( 'book-sidebar' ); }
Recommended
It is recommended to return early when the condition not met. Copy the following code and paste it to your child theme’s functions.php
// Step 1.Register new sidebar for CPT book genesis_register_sidebar( array( 'id' => 'book-sidebar', 'name' => 'Book Sidebar', 'description' => 'This is the sidebar for custom post type book.', ) ); // Step 2. Remove the primary sidebar and Add the custom one "book-sidebar" add_action('get_header','genesiskit_change_genesis_sidebar'); function genesiskit_change_genesis_sidebar() { if ( ! is_singular( 'book' ) ) { // Check if we're on a single post for my CPT called "book" return; } remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); //remove the default genesis sidebar //remove_action( 'other action', 'other do action' ); //remove adiditonal features add_action( 'genesis_sidebar', 'gk_do_sidebar' ); //add an action hook to call the function for my custom sidebar } //Step 3. Function to output my custom sidebar function gk_do_sidebar() { dynamic_sidebar( 'book-sidebar' ); }
Make sure you have a Custom post types named → book
and It’s done! Do check out the Appearance >> Widgets and here you will find the sidebar you have just registered.
Additional Tweaks includes
Post info Customization on Custom Post Type
In the 2nd step of the snippet above includes a comment out line
//remove_action( 'other action', 'other do action' ); //remove adiditonal features
This should be removed if you do not want to show additional feature on that CPT and changes required depend on the type of function you want to remove from that Custom post type.
Let suppose you not want to include default post info which shows in your default post. You can use this
remove_action( 'genesis_entry_header', 'genesis_post_info' );
and if you do not want to use footer post meta, you can use
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
and what if I need to display a different post info? Like the default 1. Published on 12/12/12 by Admin – Leave a Comment
to Custom like 2. Written on 12/12/12 and Updated on 13/13/13 – Back to CPT name
How can we achieve this?
We will use a different function for that particular Custom post type to call a different post info. Here what we can use in functions.php
//Filter the Taxonomy info on a CPT add_filter( 'genesis_post_info','themeprefix_genesis_post_info_filter', 11 ); function themeprefix_genesis_post_info_filter( $post_info ) { if ( 'book' == get_post_type() ) { //swap in CPT name $post_info='Last Updated on [post_modified_date] - <a href="/book/" class="book-list">Back to Books</a>';//swap in taxonomy and label name return $post_info; } elseif ( 'post' == get_post_type() ) { return $post_info; } }
Make sure to change the ‘book’ to your own custom post type to make it work.
Conditional Sidebar in Genesis
We can use conditional tags to display custom sidebar anywhere but i am not going to confuse you with lots of snippets.
Here we are going to show the different sidebar on Custom Post type archive page. 1. example.com/book/ < 2. example.com/book/rich-dad-poor-dad <> || means that the sidebar will show on both the single CPT post and CPT archive which is ‘book’.
is_singular( 'book' ) || is_post_type_archive( 'book' )
Well, this tag will enable you to use the same sidebar on 1. CPT archive and 2. CPT single post which is not a good idea if we are putting some details about single CPT post. So we can simply create a different sidebar for custom post type archive ‘Book’. Add this in functions.php
// Step 1.Register new sidebar for CPT Archive book genesis_register_sidebar( array( 'id' => 'book-archive-sidebar', 'name' => 'Book Archive Sidebar', 'description' => 'This is the sidebar for custom post type book Archive.', ) ); // Step 2. Remove the primary sidebar and Add the custom one "book-sidebar" add_action('get_header','wpize_change_genesis_archive_sidebar'); function wpize_change_genesis_archive_sidebar() { if ( is_post_type_archive( 'book' ) ) { // Check if we're on an archive CPT called "book" remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); //remove the default genesis sidebar add_action( 'genesis_sidebar', 'wpize_do_archive_sidebar' ); //add an action hook to call the function for my custom sidebar } } //Step 3. Function to output my custom sidebar Archive function wpize_do_archive_sidebar() { dynamic_sidebar( 'book-archive-sidebar' ); }
Result:
Still, have any problem? Don’t wait and comment your problem.