Community Forums › Forums › Archived Forums › General Discussion › Trying to add an ad widget via function with a specific paragraph number
Tagged: .content area, ad, paragraph, widget
- This topic has 2 replies, 2 voices, and was last updated 6 years, 8 months ago by
Victor Font.
-
AuthorPosts
-
June 13, 2018 at 6:57 pm #220803
David Borrink
ParticipantI wrote up a function to place an ad widget after a specified number of paragraphs in pages and in posts that have a specific category. My site is based on Genesis Sample 2.3.0. This function uses a snippet I found at https://www.doitwithwp.com/inserts-ads-after-a-few-paragraphs-wordpress/. I registered a widget area and placed the call up of the widget within the function. I'm not getting any results and I'm not sure why. I'm filtering
the_content
within a conditional of pages and posts of a specific category. I'll paste my code below. Any ideas why I'm not getting results?
http://Internal setup. No URL to share// Register adbar widget for to go after a specific paragraph on pages and only a specific category of posts register_sidebar( array( 'name' => __( 'Below Post Title (unit studies) and Page Title Ad Space', 'tto' ), 'id' => 'post-page-below-title-ad-space', 'description' => __( 'PAGES AND UNIT STUDY POSTS. Will appear after a specific number of paragraphs.', 'tto' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); // place widget after specified paragraph on pages and only posts with the unit-study-activities category if( is_page() || is_category('unit-study-activities') ) { add_filter( 'genesis_entry_content' , 'sb_widget_in_the_content' ); function sb_widget_in_the_content () { // next line is the paragraph number to specify... $show_after_p = 2; $content = apply_filters('the_content', $post->post_content); if(substr_count($content, '<p>') > $show_after_p) { $contents = explode("</p>", $content); $p_count = 1; foreach($contents as $content) { echo $content; if($p_count == $show_after_p) { ?> <div class="content-ad-space"> <?php if ( is_active_sidebar( 'post-page-below-title-ad-space' ) ) : ?> <?php dynamic_sidebar( 'post-page-below-title-ad-space' ); ?> <?php endif; ?> </div><!-- .content-ad-space--> <?php } echo "</p>"; $p_count++; } } else { the_content(); } } }
June 13, 2018 at 7:04 pm #220807David Borrink
ParticipantI wrote up a function to place an ad widget after a specified number of paragraphs in pages and in posts that have a specific category. My site is based on Genesis Sample 2.3.0. This function uses a snippet I found at https://www.doitwithwp.com/inserts-ads-after-a-few-paragraphs-wordpress/. I registered a widget area and placed the call up of the widget within the function. I'm not getting any results and I'm not sure why. I'm filtering
the_content
within a conditional of pages and posts of a specific category. I'll paste my code below. Any ideas why I'm not getting results?// Register adbar widget for to go after a specific paragraph on pages and only a specific category of posts register_sidebar( array( 'name' => __( 'Below Post Title (unit studies) and Page Title Ad Space', 'tto' ), 'id' => 'post-page-below-title-ad-space', 'description' => __( 'PAGES AND UNIT STUDY POSTS. Will appear after a specific number of paragraphs.', 'tto' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', ) ); // place widget after specified paragraph on pages and only posts with the unit-study-activities category if( is_page() || is_category('unit-study-activities') ) { add_filter( 'genesis_entry_content' , 'sb_widget_in_the_content' ); function sb_widget_in_the_content () { // next line is the paragraph number to specify... $show_after_p = 2; $content = apply_filters('the_content', $post->post_content); if(substr_count($content, '<p>') > $show_after_p) { $contents = explode("</p>", $content); $p_count = 1; foreach($contents as $content) { echo $content; if($p_count == $show_after_p) { ?> <div class="content-ad-space"> <?php if ( is_active_sidebar( 'post-page-below-title-ad-space' ) ) : ?> <?php dynamic_sidebar( 'post-page-below-title-ad-space' ); ?> <?php endif; ?> </div><!-- .content-ad-space--> <?php } echo "</p>"; $p_count++; } } else { the_content(); } } }
June 15, 2018 at 6:56 am #220856Victor Font
ModeratorDuplicate post. See https://www.studiopress.community/topic/want-to-add-an-ad-widget-after-certain-number-of-paragraphs-via-a-function/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet? -
AuthorPosts
- The topic ‘Trying to add an ad widget via function with a specific paragraph number’ is closed to new replies.