- This topic has 9 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
January 10, 2023 at 5:18 am #2490001
Carsten
Hi there, I have read your documentation on using hooks. Could you show me what I should add to exclude a specific page(s) from it’s id, like frontpage from the function below?
add_action( 'your_hook_name','example_function_name' ); function example_function_name() { ?> Insert your hook contents in here. <?php }Thanks!
January 10, 2023 at 7:05 am #2490135David
StaffCustomer SupportHi there,
you can use Conditional Tags – see here for reference:
https://codex.wordpress.org/Conditional_Tags
And for excluding pages you would do:
add_action( 'your_hook_name','example_function_name' ); function example_function_name() { if ( ! is_page( array( 12, 24, 36 ) ) ) { ?> Insert your hook contents in here. <?php } }Not the
is_pagetemplate tag where we add the array and list theids. Before the tag is!bang operator, which means NOT. So IF > NOT a page in this array do something 🙂January 10, 2023 at 9:12 am #2490429Carsten
Hi David, thanks for sharing this useful information and link to conditional tags.
Regards
CarstenJanuary 11, 2023 at 3:53 am #2491175David
StaffCustomer SupportYou’re welcome.
January 12, 2023 at 9:01 am #2493116Carsten
Hi there, just some clarification about using the Bang operator tag ! in this code. From what I can read a bang operator is connected to JS, so howcome use this in a php string?
I want to remove the sidebar on a page using this code, and by removing the bang operator it is working. But I’m not sure this is the right way to do it?
add_filter( 'generate_sidebar_layout', 'remove_search_sidebar' ); function remove_search_sidebar( $sidebar ) { if ( is_page(31017)) { return 'no-sidebar'; } return $sidebar; }Please explain this part of the code which both says return no-sidebar and return $sidebar?
{ return 'no-sidebar'; } return $sidebar; }Can I add a static page like my search page to this code, if it is not a BuddyPress page?
add_filter( 'generate_sidebar_layout', 'remove_buddypress_sidebars' ); function remove_buddypress_sidebars( $sidebar ) { if ( function_exists( 'is_buddypress' ) && is_buddypress() && ! is_page(array( 34311,31017 ) ) ) { return 'no-sidebar'; } return $sidebar; }Thanks!
January 12, 2023 at 9:42 am #2493184David
StaffCustomer Support1. That is correct. We only added the
!function as wanted our IF condition to be TRUE if the page_IDs did not match those in the Array.2. Lets expand and comment that code to explain:
function remove_search_sidebar( $sidebar ) { // Check if it is a page with id of xxxxx if ( is_page(31017) ) { // if condition is met, return no-sidebar and end the function return 'no-sidebar'; } // if the condition is NOT met, return the current theme setting and end the function return $sidebar; }When the
returnis fired, it does two things, 1) it returns the value 2) it ends the function there and then.
So if theif ( is_page(31017) ) {is FALSE it will move ontoreturn $sidebar;The
$sidebarvariable is whatever that page has set currently.2. So do you want it to work on buddy press pages and some non-buddypress pages ?
January 12, 2023 at 10:22 am #2493238Carsten
Thank you for the explanation.
1. Could we use the True False statement instead, and what decides when to use this or the bang operator?
2. Yes my thought was to put it all in one code, BP pages and non BP static pages together, instead of having two set of codes with same purpose.
January 13, 2023 at 3:04 am #2494010David
StaffCustomer Support1. Heres an example, if i have 100 posts, and i would like to DO SOMETHING to just 97 of them.
I could target all 97 posts in my Array:if ( is_page( array( 1, 2, 3, 4, .... 95, 96, 97 ) ) ) {But that would be crazy as i would have to write 97 IDs in my function.
So instead you can use the ! bang operator, so you just list the IDs that you DO NOT WANT TO DO SOMETHING with.
if ( !is_page( array( 98, 99, 100 ) ) ) {But it was probably a bad example and we should go back…
2. Try this function:
add_filter( 'generate_sidebar_layout', 'remove_buddypress_sidebars' ); function remove_buddypress_sidebars( $sidebar ) { // Array of page IDs that you want NO sidebar on $no_sidebar_pages = array( 123, 456 ); if ( ( function_exists( 'is_buddypress' ) && is_buddypress() ) // if it is buddypress || is_page( $no_sidebar_pages ) ) // OR if it is a $no_sidebar_pages ) { return 'no-sidebar'; } return $sidebar; }This one you can update the IDs here:
$no_sidebar_pages = array( 123, 456 );That way you don’t have to mess with the IF statement.
January 13, 2023 at 3:07 am #2494014Carsten
Hi there, thanks again for your good explanations 😉
Regards
CarstenJanuary 13, 2023 at 5:33 am #2494120David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.