Community Forums › Forums › Archived Forums › Design Tips and Tricks › Conditional Action Hook Question
- This topic has 5 replies, 2 voices, and was last updated 9 years, 9 months ago by
nomis.
-
AuthorPosts
-
May 23, 2015 at 11:11 am #153423
nomis
MemberHi again,
I’m working with a Genesis theme which uses Action Hooks to add and remove widget areas. My site divides posts into two main categories. The two blogs are completely separate from each other and are accessed by separate main menu items. Clicking on “Blog” takes you to a normal blog, and clicking on “Tutorials” takes you to a portfolio template page which leads through to completely separate tutorial posts.
So far I have registered two widget areas to the ‘genesis_after_header' hook. One widget appears on the Blog Page, and the other appears on the Tutorials Page. They both contain custom menus. This means that when you're in the Blog, you can filter the blog categories, and when you’re in Tutorial you can filter the tutorial categories. The Tutorial Submenu filters by removing categories from the portfolio page. This is the code that I’m using, and it works just fine:
// **************************** BLOG SUBMENU WIDGET **************************** //* rise - Register blog-submenu widget area genesis_register_sidebar( array( 'id' => 'blog-submenu', 'name' => __( 'Blog Submenu', 'epik' ), 'description' => __( 'Place submenu for Blog Page here', 'epik' ), ) ); //* rise - Hook blog-submenu widget area after header add_action( 'genesis_after_header', 'add_blog_submenu' ); function add_blog_submenu() { if (is_singular(post) && is_category('Blog') || is_category('Blog_Web') || is_category('Blog_Photos') || is_category('Blog_General') || is_page('Blog')) genesis_widget_area ('blog-submenu', array ( 'before' => '<div class="wrap"><div class="blog-submenu widget-area">', 'after' => '</div></div>', ) ); } // ***************************** TUTS SUBMENU WIDGET *************************** //* rise - Register tuts-submenu widget area genesis_register_sidebar( array( 'id' => 'tuts-submenu', 'name' => __( 'Tuts Submenu', 'epik' ), 'description' => __( 'Place submenu for Tuts Page here', 'epik' ), ) ); //* rise - Hook tuts-submenu widget area after header add_action( 'genesis_after_header', 'add_tuts_submenu' ); function add_tuts_submenu() { if (is_page('Illustrator Tutorials') || is_page('Sublime Text Tutorials') || is_page('Photoshop Tutorials') || is_page('Tutorials') || is_category('Tuts_PS') || is_category('Tuts_Ill') || is_category('Tuts_ST')) genesis_widget_area ('tuts-submenu', array ( 'before' => '<div class="wrap"><div class="tuts-submenu widget-area">', 'after' => '</div></div>', ) ); }
However, the filter menus (Submenus) were not showing up on single posts. So I added (is_singular('post') to the Blog Submenu code:
//* rise - Hook blog-submenu widget area after header add_action( 'genesis_after_header', 'add_blog_submenu' ); function add_blog_submenu() { if (is_singular(post) || is_category('blog_web') || is_category('blog_photos') || is_category('blog_general') || is_page('blog')) genesis_widget_area ('blog-submenu', array ( 'before' => '<div class="wrap"><div class="blog-submenu widget-area">', 'after' => '</div></div>', ) ); }
This worked fine, but the blog-submenu was now also showing on the Tutorial single posts. So I added (is_singular(post) && is_category(‘blog'). My thought was that this would only add the blog-submenu to single posts that also had the Blog category attached to them.
//* rise - Hook blog-submenu widget area after header add_action( 'genesis_after_header', 'add_blog_submenu' ); function add_blog_submenu() { if (is_singular(post) && is_category('blog') || is_category('blog_web') || is_category('blog_photos') || is_category('blog_general') || is_page('blog')) genesis_widget_area ('blog-submenu', array ( 'before' => '<div class="wrap"><div class="blog-submenu widget-area">', 'after' => '</div></div>', ) ); }
But this made the blog-submenu disappear from ALL single posts. So I added this (is_singular(post) && !is_category('Tutorials'). My thought was that this would only add the blog-submenu to single posts that did NOT have the Tutorials category attached to them.
//* rise - Hook blog-submenu widget area after header add_action( 'genesis_after_header', 'add_blog_submenu' ); function add_blog_submenu() { if (is_singular(post) && !is_category('Tutorials') || is_category('Blog_Web') || is_category('Blog_Photos') || is_category('Blog_General') || is_page('Blog')) genesis_widget_area ('blog-submenu', array ( 'before' => '<div class="wrap"><div class="blog-submenu widget-area">', 'after' => '</div></div>', ) ); }
This did not remove the blog-submenu from single posts that had the Tutorials category attached.
So basically, I want to add the blog-submenu widget to single posts that have the Blog category, but not the Tutorials category. Can anyone help?
May 23, 2015 at 1:11 pm #153430Graham
MemberI would need to double check your logic, so bear with me, yet for now ..
is_singular() parameter is either string || array
is_singular( post )
should be
is_singular( 'post' )
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 23, 2015 at 2:04 pm #153433Graham
Memberis_category() checks if a category archive page is being displayed.
To test if a post is in a category use in_category()So basically, I want to add the blog-submenu widget to single posts that have the Blog category, but not the Tutorials category. Can anyone help?
if( is_singular('post') && in_category('Blog') { // show blog-submenu }
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 23, 2015 at 3:02 pm #153437nomis
MemberSuperb !!!
This solved my problem perfectly - and I learned something 🙂
Thanks soooooooooo much Graham.si
May 23, 2015 at 3:20 pm #153438Graham
MemberGlad it helped 🙂
I can't edit my post, so I've corrected my typo from above here (missing closing bracket ) for future copy pasta.
if( is_singular('post') && in_category('Blog')) { // show blog-submenu }
My JustGiving page: https://www.justgiving.com/helping-graham-give
May 24, 2015 at 5:04 am #153462nomis
MemberHave attempted to copy pasta.
Tagliatelle now jammed in photocopier.
Is copying pasta essential to working with Genesis? -
AuthorPosts
- The topic ‘Conditional Action Hook Question’ is closed to new replies.