Wordpress show only on posts - php

i have a problem with my WP function. I need it to ONLY be shown on blog posts. Right now it get shown on the other pages such as the frontpage.
Here is my function in functions.php:
function my_sp_function( $content ) {
$include = FALSE;
$post = FALSE;
if ( in_category( array( 3, 4, 5, 6, 91, 133 ) ) ) {
$include = TRUE;
}
if (get_post_type(get_the_ID()) == 'post'){
$post = TRUE;
}
if ( $post = TRUE AND $include == TRUE ) {
return $content;
}
}
Anybody there knows what i do wrong?
I have tried with this code now, it still shows the $content on the front page..
function my_sp_function( $content ) {
$include = FALSE;
$post = FALSE;
if ( in_category( array( 3, 4, 5, 6, 91, 133 ) ) ) {
$include = TRUE;
}
if( is_home() || is_front_page() ) {
$include = FALSE;
}
if ( $post == TRUE AND $include == TRUE ) {
return $content;
}
}

If you would like to include your function only on the home page you have two options - only insert the function into the single.php template in your theme, or the prefered method is to only hook your function to the_content when is_single() is true but is_page() is false.
function my_sp_function( $content ) {
$content .= "Some more stuff for the_content!";
return $content;
}
if ( is_single() && ! is_page() ){
add_filter( 'the_content', 'my_sp_function' );
}

Related

How to exclude specific post from wp functions.php code?

I used this code to show responsive ad from adsense after 5th paragraph.
Adding Ads After First And Second Paragraph of WordPress Post
This is the code I use on my site:
function prefix_insert_after_paragraph2( $ads, $content ) {
if ( ! is_array( $ads ) ) {
return $content;
}
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
$n = $index + 1;
if ( isset( $ads[ $n ] ) ) {
$paragraphs[$index] .= $ads[ $n ];
}
}
return implode( '', $paragraphs );
}
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph2( array(
// The format is: '{PARAGRAPH_NUMBER}' => 'AD_CODE',
'5' => '<div>Ad code after FIRST paragraph goes here</div>',
), $content );
}
return $content;
}
I would like to exclude this code only for specific posts. How can I add the proper code to be able to exclude this function for post id=280?
Thank you.
Why do you want to exclude this function inside functions.php?
I think it's much easier to do it inside the post loop.
For example, to exclude the POST ID 280, if you're inside the page.php, or similar you can do this:
global $post;
if ($post->ID == 280){ remove_filter( 'the_content', 'prefix_insert_post_ads' ); }
This way you have the 'add_filter' on your functions.php file, and if you find the exception inside the post (ID=280), you remove the filter.
Just U have to check the current post_id. E.g. :
function prefix_insert_post_ads( $content ) {
global $post;
$post_id = $post->ID;
$post_ids_excluded = [280,....]; // excluded posts ids
if ( in_array($post_id,$post_ids_excluded) ){
return $content;
}
/*
... the same code .....
*/
}

Body class on forum page

I'm using this code to show category on body class.
But now I'm facing problems on BBPress forum page. I'm getting Notices "non-object, undefined offset..."
How to exclude BBPress pages from this? I need to have just normal body classes when on forum pages.
add_filter('body_class','top_cat_body_class');
function top_cat_body_class($classes) {
if( is_single() ) :
global $post;
$cats = get_the_category( $post->ID );
if( count( $cats ) > 1 ) {
return array('genericClass');
}
else {
$cat_anc = get_ancestors( $cats[0]->term_id, 'category' );
$top_cat = array_merge( array($cats[0]->term_id), $cat_anc );
$top_cat = array_pop( $top_cat );
return array(get_category($top_cat)->slug);
}
elseif( is_category() ) :
$cat_anc = get_ancestors( get_query_var('cat'), 'category' );
$top_cat = array_merge( array(get_query_var('cat')), $cat_anc );
$top_cat = array_pop( $top_cat );
return array(get_category($top_cat)->slug);
else :
return $classes;
endif;
}
You can use bbPress Conditional Tags and add to the beginning of your top_cat_body_class function code something like
if (function_exists('is_bbpress') AND is_bbpress()) return $classes;
Hope it helps!

How to add ad code in wordpress Single page without affecting the second single page template

I am using two different template for single page. One with sidebar and another one without sidebar.
Now i want to inject the ad code only in the sidebar template.
Could any one give me a code for this one.
Note:
The ad is automatically inserted after every second paragraph. And i have the code.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
if ( is_single() && ! is_admin()) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
The above code display the ad code both in sidebar template and no sidebar template. But actually i want to display the ad code only in sidebar template. Could any one please help me how to do it.
You can check which template is used with get_page_template_slug():
if ( is_single() && ! is_admin() && get_page_template_slug() == 'template-sidebar.php') {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
You can use this code on your single php file
if (is_page(12)) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
Thanks.

How to run a function instead of text in this wordpress plugin?

Here is a simple code which shows the Adsense or any ad code after second paragraph on single post page. But I want to know how to modify it so that it can run a dynamic function.
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
In fact I want to replace
$ad_code = '<div>Ads code goes here</div>';
with
$ad_code = '<?php if(function_exists('echo_ald_crp')) echo_ald_crp(); ?>';
Is there any way to do it??
Well, you can't assign PHP to a string var and expect it to execute. You could create a function with the code in it, have it return something, and then set the variable equal to the function call. Like this:
function output_function() {
//do something here
return $thing_you_want_to_return;
}
function prefix_insert_post_ads( $content ) {
$ad_code = output_function();
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
That way, when the variable is assigned, it'll run your function and return what you want to be echoed out with prefix_insert_after_paragraph().

php wordpress function, explanation on string in single quotes

Looking at the file in my editor it's a function and inside the function is a string in single quotes.
I have this wordpress function like so:
<div class="site-info">
<?php do_action( 'focus_credits' ); ?>
</div><!-- .site-info -->
When i inspect element in chrome it's like this:
<div class="site-info">
Theme By yada yada yada and a link to their website here </div>
My question is how is the literal string 'focus credits' returning the name of the theme and an href link to their website.
I've discovered the do_action function in the plugin.php
function do_action($tag, $arg = '') {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
if ( ! isset($wp_actions[$tag]) )
$wp_actions[$tag] = 1;
else
++$wp_actions[$tag];
// Do 'all' actions first
if ( isset($wp_filter['all']) ) {
$wp_current_filter[] = $tag;
$all_args = func_get_args();
_wp_call_all_hook($all_args);
}
if ( !isset($wp_filter[$tag]) ) {
if ( isset($wp_filter['all']) )
array_pop($wp_current_filter);
return;
}
if ( !isset($wp_filter['all']) )
$wp_current_filter[] = $tag;
$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
$args[] =& $arg[0];
else
$args[] = $arg;
for ( $a = 2; $a < func_num_args(); $a++ )
$args[] = func_get_arg($a);
// Sort
if ( !isset( $merged_filters[ $tag ] ) ) {
ksort($wp_filter[$tag]);
$merged_filters[ $tag ] = true;
}
reset( $wp_filter[ $tag ] );
do {
foreach ( (array) current($wp_filter[$tag]) as $the_ )
if ( !is_null($the_['function']) )
call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
} while ( next($wp_filter[$tag]) !== false );
array_pop($wp_current_filter);
}
so his 'focus_credits' calling some text from the DB?
I looked through some of the tables but couldn't find anything relating..
Any help where to look would be great. Thanks in advance.
do_action() inserts an action hook in your template. If you dig through your theme's files, possibly functions.php, you should find a function that is hooked to this action hook:
add_action( 'focus_credits', 'focus_credits' );
function focus_credits(){
echo 'Theme By yada yada yada and a link to their website here';
}
More info on do_action(): http://codex.wordpress.org/Function_Reference/do_action

Categories