• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Trying to add an ad widget via function with a specific paragraph number

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Trying to add an ad widget via function with a specific paragraph number

This topic is: not resolved

Tagged: .content area, ad, paragraph, widget

  • This topic has 2 replies, 2 voices, and was last updated 5 years, 3 months ago by Victor Font.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • June 13, 2018 at 6:57 pm #220803
    David Borrink
    Participant

    I 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();
            }
    
        }
        
        }
    http://Internal setup. No URL to share
    June 13, 2018 at 7:04 pm #220807
    David Borrink
    Participant

    I 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 #220856
    Victor Font
    Moderator

    Duplicate 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?

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Trying to add an ad widget via function with a specific paragraph number’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble