Archived topic
Excluding pages from hook
9 replies · Started by Carsten on January 10, 2023
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!
Hi 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_page template tag where we add the array and list the ids. Before the tag is ! bang operator, which means NOT. So IF > NOT a page in this array do something :)
Hi David, thanks for sharing this useful information and link to conditional tags.
Regards
Carsten
You're welcome.
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!
1. 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 return is fired, it does two things, 1) it returns the value 2) it ends the function there and then.
So if the if ( is_page(31017) ) { is FALSE it will move onto return $sidebar;
The $sidebar variable is whatever that page has set currently.
2. So do you want it to work on buddy press pages and some non-buddypress pages ?
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.
1. 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.
Hi there, thanks again for your good explanations ;-)
Regards
Carsten
You're welcome