Community Forums › Forums › Archived Forums › Design Tips and Tricks › Exclude Blog Page when Adding an Action / Remove Action from Blog Page (NewsPro)
Tagged: Add Action, condition, identify blog page, remove action
- This topic has 2 replies, 2 voices, and was last updated 6 years, 3 months ago by nici-.
-
AuthorPosts
-
January 11, 2018 at 9:08 am #215297nici-Member
Hi,
I need to remove an action from the blog page of NewsPro theme. Therefor I need a condition in function.php to identify this page. Or - even better: A condition with which I can add my action ONLY to the single view of posts.
What I have done and want to do:
I use NewsPro theme here:
https://shopanbieter.de/relaunchI added an action that displays a random sponsor ad into the blog posts hooked into 'genesis_before_entry_content'. So far so good:
************
// zufällige Sponsor-Ad in die Posts - physalia.de
add_action( 'genesis_before_entry_content', 'SponsorAds' );
function SponsorAds() {global $post;
if($post->post_type == 'post'){print random_file_content(ABSPATH . "SponsorAds");}
}function random_file_content($dir)
{
$files = scandir($dir); # since php5
$files = array_diff($files, [".", ".."]);$key = array_rand($files);
$file = $files[$key];
return file_get_contents($dir."/".$file);
}
**************To prevent the ads from the posts of one special category, I removed the action this way:
**************
// zufällige Sponsor-Ad NICHT in Sponsored Posts-Kategorie - physalia.de
function NoSponsorAd( $query ) {
if ( $query->is_single() ) {
$query->set( 'cat', '-21' );//add your category number
}
return $query;
}
remove_action( 'genesis_before_entry_content', 'SponsorAds' );**************
Also ok.
Now I created the blog page:
(like in the manual this is an empty page with template "Blog")Unfortunately the ads show in this page! I do NOT want the ads here. So first I tried to also check for single view in the condition when adding the action - but to no avail.
I Asked the support, they sent me this code to remove the action, but this does NOT work:
**********
// unhook SponsorAds only when page with blog page template is in use
add_action( 'get_header', 'remove_SponsorAds_blog_page' );
function remove_SponsorAds_blog_page() {
if ( is_page_template(page_blog-php) ) {
remove_action( 'genesis_before_entry_content', 'SponsorAds' );
}
}
**********Pity!
So I tried to check on the single template when adding the action:
**********
add_action( 'genesis_before_entry_content', 'SponsorAds' );
function SponsorAds() {global $post;
if($post->post_type == 'post' && is_page_template('single.php') ){print random_file_content(ABSPATH . "SponsorAds");}
}
**********Still the ads are shown in the blog page:
Question:
How can I remove the ads here (or even better not add them there in the first place)? All hints are appreciated.(I've just started with Genesis framework and are a bit weak in skills so far - but learning quickly I hope).
Best regards
https://www.shopanbieter.de/relaunch/news/
nici-January 11, 2018 at 10:11 am #215299MarcyParticipantThe conditional for the blog page template is:
if ( genesis_is_blog_template() ) { ... }
Marcy | Amethyst Website Design | Twitter
January 15, 2018 at 3:00 pm #215430nici-MemberGreat, thanks a lot!
-
AuthorPosts
- The topic ‘Exclude Blog Page when Adding an Action / Remove Action from Blog Page (NewsPro)’ is closed to new replies.