admin管理员组文章数量:1305072
SOLVED: Because I overlooked a basic feature I didn't realize existed. LOL. Veteran brain-lapse.
My attempt at something sort of works, and could I ask for a fresh set of eyes to see what I'm missing? Thanks.
I'm trying to create some conditional sidebars using Genesis Simple Sidebars, and modifying a code snippet from WPBeaches which was posted in 2014. What I want to do is have a few custom sidebars dedicated to a few specific category archives and their single posts.
I created a single test widget in each custom sidebar, wrote up the code to use two different categories with conditionals (using is_category for archive pages and in_category for single posts), and a fallback of the primary sidebar for all other categories. The result is that the sidebars work on those two categories (and one of them is a parent category which I want to cascade to its child categories), but on single posts in those two categories the result is the primary sidebar. The child category archives of the one parent category do not show the custom sidebar either. So it sort of works, but not as I expected.
I have a conditional of is_single, is_category, and is_tag to start the process. Here is the code.
// Custom sidebars with conditionals for post categories
add_action( 'genesis_before_sidebar_widget_area', 'themeprefix_remove_sidebar' ); // starts the ball rolling
function themeprefix_remove_sidebar() {
if ( is_single() || is_category() || is_tag() ) { // set your connditionals here
remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); // removes Simple Sidebar
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); // removes Genesis Default sidebar
add_action( 'genesis_sidebar', 'sb_add_sidebar' ); // adds alternative sidebar in function below
}
}
// Alternative Sidebar
function sb_add_sidebar() {
if( is_category('christian-faith') || in_category('christian-faith') ) {
dynamic_sidebar( 'faith-sidebar' );
}
elseif( is_category('classroom') || in_category('classroom') ) {
dynamic_sidebar( 'teaching-in-the-classroom-sidebar' );
}
else {
dynamic_sidebar( 'sidebar' );
}
}
And here are links to the two example category pages.
/
Oe the first I have two identical test widgets. On the second one, I put the test widget first and my wife put a bunch of widgets in after it. The result is that the primary sidebar shows up, and then after that's done (the last widget is "our history timeline"), the custom sidebar starts with the test widget and the set she added.
So why I'm getting double widgets on one, and two different sets on the other, I do not know. Obviously there's something I don't understand here.
SOLVED: Because I overlooked a basic feature I didn't realize existed. LOL. Veteran brain-lapse.
My attempt at something sort of works, and could I ask for a fresh set of eyes to see what I'm missing? Thanks.
I'm trying to create some conditional sidebars using Genesis Simple Sidebars, and modifying a code snippet from WPBeaches which was posted in 2014. What I want to do is have a few custom sidebars dedicated to a few specific category archives and their single posts.
I created a single test widget in each custom sidebar, wrote up the code to use two different categories with conditionals (using is_category for archive pages and in_category for single posts), and a fallback of the primary sidebar for all other categories. The result is that the sidebars work on those two categories (and one of them is a parent category which I want to cascade to its child categories), but on single posts in those two categories the result is the primary sidebar. The child category archives of the one parent category do not show the custom sidebar either. So it sort of works, but not as I expected.
I have a conditional of is_single, is_category, and is_tag to start the process. Here is the code.
// Custom sidebars with conditionals for post categories
add_action( 'genesis_before_sidebar_widget_area', 'themeprefix_remove_sidebar' ); // starts the ball rolling
function themeprefix_remove_sidebar() {
if ( is_single() || is_category() || is_tag() ) { // set your connditionals here
remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); // removes Simple Sidebar
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); // removes Genesis Default sidebar
add_action( 'genesis_sidebar', 'sb_add_sidebar' ); // adds alternative sidebar in function below
}
}
// Alternative Sidebar
function sb_add_sidebar() {
if( is_category('christian-faith') || in_category('christian-faith') ) {
dynamic_sidebar( 'faith-sidebar' );
}
elseif( is_category('classroom') || in_category('classroom') ) {
dynamic_sidebar( 'teaching-in-the-classroom-sidebar' );
}
else {
dynamic_sidebar( 'sidebar' );
}
}
And here are links to the two example category pages.
https://sallieborrink/category/christian-faith
https://sallieborrink/category/classroom/
Oe the first I have two identical test widgets. On the second one, I put the test widget first and my wife put a bunch of widgets in after it. The result is that the primary sidebar shows up, and then after that's done (the last widget is "our history timeline"), the custom sidebar starts with the test widget and the set she added.
So why I'm getting double widgets on one, and two different sets on the other, I do not know. Obviously there's something I don't understand here.
Share Improve this question edited Feb 15, 2021 at 16:21 David Borrink asked Feb 13, 2021 at 2:06 David BorrinkDavid Borrink 234 bronze badges1 Answer
Reset to default 0Sometimes you don't realize that built-in features exist, even when you're a veteran user. I had overlooked that categories have a sidebar choice feature.
No wonder nobody answered.
本文标签: categoriesHow do I get conditional sidebars for specific category archives and posts
版权声明:本文标题:categories - How do I get conditional sidebars for specific category archives and posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741748212a2395666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论