Notifications
Clear all

Coding to inject an AD after the 1st paragraph works only on the 1st page. How could I extend it to all sub-pages?

6 Posts
2 Users
0 Reactions
2,585 Views
(@info1)
Eminent Member
Joined: 10 years ago
Posts: 26
Topic starter  

Dear Support,

I just started to use your ACP plugin.

It works very well, but I noticed a problem.

With this coding inside 'functions.php':

//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 = '<!-- 300x250 -->';
	    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 );
	}

I'm injecting an ad after the 1st text paragraph of my articles.

After enabling your plugin and adding the necessary shortcodes, I see that this AD shows up only on the 1st page.

Is there anything we could do to have the AD show up also inside all the sub-pages?

Thanks,
Mihai


   
Quote
 Tom
(@tomson)
Famed Member Admin
Joined: 10 years ago
Posts: 4214
 

Hi Mihai,
It seems you use pagination Ajax loading type. This type doesn't allow you to do this injection. It can be possible if you change the loading type to simple/refresh. You can do that in Dashboard > AP Pagination, just set "Pagination loading type" as "Reload Page".

Please try with this option and let us know if it works.


   
ReplyQuote
(@info1)
Eminent Member
Joined: 10 years ago
Posts: 26
Topic starter  

Hi Tom,

Thanks for your feedback!

What I am using is not the "Ajax" loading type, but the "Reload Page" one because my goal is to generate more page views.

Any idea what coding should I do instead of the above?

Or maybe there is something you may like to add to the plugin to make this possible.

Kind Regards,

Mihai


   
ReplyQuote
(@info1)
Eminent Member
Joined: 10 years ago
Posts: 26
Topic starter  

Let me add that this last feature request that I just mentioned is for the rest of the page, more specifically the top of the page even above the main title (h1), not the 'acp_wrapper' loop.


   
ReplyQuote
(@info1)
Eminent Member
Joined: 10 years ago
Posts: 26
Topic starter  

Hi Tom,

Apart from the above, I would like to ask you about something else, maybe a new feature for this plugin.

At the top of my articles I'm using this coding:

<div class="entry-featured">
	 <?php x_featured_image(); ?>
	</div>

This is displaying the featured image, which is an image as wide as the layout.

I would like the ability to display that image only on the 1st page.

At the moment this shows up across all pages, so also on the /2, /3, /4 etc. sub-pages.

When you build this capability it would be nice to be able to add custom stuff based on the page number.

So for this request of mine I would use page 1, but maybe some other people would like to display something on other pages.

A good idea could be to let people specify the page number or things like: 'first' or 'last'.

In other words somebody may want to add a text on the very last sub-page, say a link to a specific page or section.

Thanks,
Mihai


   
ReplyQuote
(@info1)
Eminent Member
Joined: 10 years ago
Posts: 26
Topic starter  

So what I could think about is something like this logic:

1) If this is a page using NEXTPAGE (so using your pluin);

2) If this page is the 'first' page, then display this:

<div class="entry-featured">
	 <?php x_featured_image(); ?>
	 </div>

Thanks,
Mihai


   
ReplyQuote
 Tom
(@tomson)
Famed Member Admin
Joined: 10 years ago
Posts: 4214
 

Ok, thank you for the good idea, I think it's possible to add subPage content managing options in this plugin. However the only valuable option I see now is the "Show Featured Image - yes/no". Have you other suggestion for this?

Regarding to a code injection into ACP subPages, please try this, maybe the $content returns the whole content:

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
	    $subPages = explode('[nextpage', $content);
	    if( !empty($subPages) ){
	        $new_content = '';
	        foreach( $subPages as $subPage ){
	            $closing_p = '</p>';
	            $paragraphs = explode( $closing_p, $subPage );
	            foreach ($paragraphs as $index => $paragraph) {
	                if ( trim( $paragraph ) ) {
	                    $paragraphs[$index] .= $closing_p;
	                }
	                if ( $paragraph_id == $index + 1 ) {
	                    $paragraphs[$index] .= $insertion;
	                }
	            }
	            $new_content[] = implode( '', $paragraphs );
	        }
	        return implode( '[nextpage', $new_content);
	    }
	    else{
	        return $content;
	    }
	}

I'd also recommend set high priority for your function:

add_filter( 'the_content', 'prefix_insert_post_ads', 1 );

   
ReplyQuote
Share:
Scroll to top