- This topic has 8 replies, 3 voices, and was last updated 4 years, 2 months ago by Tom.
-
AuthorPosts
-
February 12, 2020 at 8:40 pm #1163656Khaled
Hi,
I’m still finalizing customizing the theme. I got a couple of issues. For my previous theme, I use the following code in function to insert Adsense after 2nd paragraph. Please have a look and tell me if i need to change anything//Insert ads after the second paragraph of single post content.
add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );
function prefix_insert_post_ads( $content ) {
$ad_code = ‘<!– Ezoic – article_2nd_paragraph – under_second_paragraph –>
<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
<ins class=”adsbygoogle”
style=”display:block; text-align:center;”
data-ad-format=”fluid”
data-ad-layout=”in-article”
data-ad-client=”ca-pub-xxxxxxxxxxxxx”
data-ad-slot=”xxxxxxxxxx”></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<!– End Ezoic – article_2nd_paragraph – under_second_paragraph –>‘;
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 );
}Another issue is regarding amp auto ads – For amp auto ads what do i need to change in the following code and insert in function
add_action(‘amp_post_template_head’,’amp_my_custom_head’);
function amp_my_custom_head($amp_template) {
?>
<script async custom-element=”amp-auto-ads” src=”https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js”> </script>
<?php
}add_action(‘amp_post_template_footer’,’amp_my_custom_footer’);
function amp_my_custom_footer($amp_template) {
?>
<amp-auto-ads type=”adsense”
data-ad-client=”ca-pub-xxxxxxxxxxxxx”>
</amp-auto-ads>
<?php
}February 13, 2020 at 8:43 am #1164318TomLead DeveloperLead DeveloperHi there,
All of those functions should work in GP just like they did in your previous theme.
Are they not working at the moment? If so, what kind of errors are you getting?
February 13, 2020 at 8:53 am #1164342KhaledThe first function working fine. However, how can i modify the code to output a different amps ad when page is in amp. Like the following – $ad_code = ‘regular Adsense ad code”;
to
<?php
if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?><amp-ad code>
</amp-ad><?php } else { ?>
Regular ad code<!– End Ezoic – Below_article_title – under_page_title –>
<?php } ?>For the second function to enable amp auto ads not working. But I use the following code which works fine
// ads for amp – will be loaded into header
$print_amp_auto_ads = function() {
$ad_client = ‘ca-pub-xxxxxxxxxxxxxx’; // YOUR Publisher ID goes here
if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) {
?>
<amp-auto-ads type=”adsense” data-ad-client=”<?php echo esc_attr( $ad_client ); ?>”></amp-auto-ads>
<?php
}
};
// For Paired/Native mode.
add_action( ‘wp_footer’, $print_amp_auto_ads );// ads for amp end
February 13, 2020 at 4:14 pm #1164658TomLead DeveloperLead DeveloperThat code (
is_amp_endpoint()
) is exactly how you would do it.Not seeing anything wrong with the second function, either. Looks good 🙂
February 13, 2020 at 7:58 pm #1164778KhaledOk, but placing the code as following break the site.
$ad_code = ‘<?php
if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?><amp-ad code>
</amp-ad>
<?php } else { ?>Regular ad code
<!– End Ezoic – Below_article_title – under_page_title –>
<?php } ?>’;February 14, 2020 at 9:44 am #1165562TomLead DeveloperLead DeveloperWhy are you using the
$ad_code
variable?Try this:
<?php if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?> <amp-ad code> </amp-ad> <?php } else { ?> Regular ad code <!– End Ezoic – Below_article_title – under_page_title –> <?php } ?>
February 14, 2020 at 8:45 pm #1165834Khaled$ad_code is part of the parent function to execute ad code after first paragraph of the content. I tried the following code but not working —
//Insert ads after the second paragraph of single post content.
add_filter( ‘the_content’, ‘prefix_insert_post_ads’ ); function prefix_insert_post_ads( $content ) { $ad_code = '<?php if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?> <amp-ad code> </amp-ad> <?php } else { ?> Regular ad code <!– End Ezoic – Below_article_title – under_page_title –> <?php } ?>'; 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 );
}February 14, 2020 at 9:16 pm #1165841Suraj KatwalHere is a code to insert ads after a certain paragraph.
add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code = ' Paste your AdSense Code Inside this quote'; if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 4, $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 ); }
More at insert Google AdSense Ads in GeneratePress without Plugin
February 15, 2020 at 9:04 am #1166346TomLead DeveloperLead DeveloperLet’s try this, instead:
add_filter( 'the_content', function( $content ) { ob_start(); if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { ?> <amp-ad code> </amp-ad> <?php } else { ?> Regular ad code <!– End Ezoic – Below_article_title – under_page_title –> <?php } $ad_code = ob_get_clean(); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 2, $content ); } return $content; } );
-
AuthorPosts
- You must be logged in to reply to this topic.