Skip Navigation

[Résolu] Insert script into view

This support ticket is created Il y a 6 années et 4 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Marqué : ,

This topic contains 15 réponses, has 3 voix.

Last updated by Beda Il y a 5 années et 3 mois.

Assisted by: Beda.

Auteur
Publications
#597271

Nigel,

Is it possible to add an adsense script into the middle of a view? My view is displaying 30 posts per page and I'd like to insert an adsense leaderboard ad in the middle of the page.

Here's a screenshot of an example: hidden link

Thanks,
Chuck

#597330

The View's loop will iterate though each item in it, and add things that you add within the loop, for each item.

The only way to address a certain location within the View loop, is to use the wpv-item shortcode:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-item

With this you can address particular positions (index) in the Loop.

#597579

Thanks Beda.

I tired "index=split2", but it wasn't giving me the desired result, so as a work around I "split the view" by duplicating the view into top and bottom views, and inserted my ad manually in between the two views.

For the top view I displayed the first 16 posts and the following 16 are displayed through the other view. However, my pagination button is no longer visible for the bottom view. Not sure why as the only thing I changed was the limit and offset settings. Display: 16, Skip: first 16.

Thanks,
Chuck

#597591

Here's a screenshot: hidden link

It looks to be hidden.

Thanks,
Chuck

#598091

index=split2 will target the item that falls in the middle of the number of displayed items.
In a View displaying 10 or 11 items (set by a limit or pagination), this will target the 5th issue (because it is a “floored middle”).

That is the correct approach, and it works.

What is not working as you expect on your views?

By splitting the View you seem to have achieved the desired result, but this is also possible with above ShortCode attribute.

Why the pagination is hidden I am not sure. If you insert this second View to another, new page, is the pagination there?
If not, it's either because there is nothing to paginate or because it's removed with JS or CSS (rather JS, since the entire HTML is missing on your end)

Please let me know the outcome of above tests.

#598254

Beda,

The original view and pagination button can be viewed here: hidden link

Then I duplicated that view and used it here, keeping the same pagination markup, and only changing the number of posts displayed: hidden link

This is the pagination HTML markup I am using for both views pagination:

[wpv-filter-start hide="false"]
[wpv-filter-controls][/wpv-filter-controls]
<div class="pagbutton">
  [wpv-pagination]
    <div class="nextbutton">
      [wpv-pager-next-page force="true"][wpml-string context="wpv-views"]View More[/wpml-string][/wpv-pager-next-page]
    </div>
  [/wpv-pagination]
</div>
  [wpv-post-body view_template="adsense-bottom"]
[wpv-filter-end]

Originally I tried using index=split2, but I could not get it to work properly.
This is the markup I tried. Please correct me if I’m wrong, as I would rather use one view instead of two:

[wpv-layout-start]
  [wpv-post-body view_template="adsense-main-top"]
[wpv-items-found]
<!-- wpv-loop-start -->
	<wpv-loop wrap="2" pad="true">
		[wpv-item index=1]
		<div class="row fluid">
			<div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
		[wpv-item index=other]
			<div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
		[wpv-item index=2]
			<div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
                [wpv-item index=split2]
	                <div class="col-sm-12"> [wpv-post-body view_template="adsense-main-middle"]</div>
		</div>
		[wpv-item index=pad]
			<div class="col-sm-6"></div>
		[wpv-item index=pad-last]
			<div class="col-sm-6"></div>
		</div>
	</wpv-loop>
<!-- wpv-loop-end -->
    
[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Thank you,
Chuck

#598423

This is the loop to target only the middle item:

	<wpv-loop>
	  [wpv-item index=1]
		[wpv-post-title]
	  [wpv-item index=split2]
        [wpv-post-title] + This is the middle   
	  [wpv-item index=other]
      	[wpv-post-title]
	</wpv-loop>

It will output only the post title, unless for the middle item, it will output the post title along with the string "+ This is the middle" or anything else of your choice.

When you use pagination it will split each list into 2, this means, you will see the ad added in the middle item of each pagination page.
When you have 30 posts, and paginate with 10 each, and have a split2 in the loop, then the front end will show 10 items each paginated item, and within them, in the middle (after the 5th post) will be added the ad.

I tested this extensively to ensure the current expectation.
If this is not working on your install it's either because of some compatibility issue (by disabling other software we would find out) or it's a specific setting you use and I asm not aware of.

Please ket me know the outcome, and if it's solved without other software.
If not, please send me your Views, exported from the Import/Export page of Toolset.

Thank you

#599518

Beda,

I'm still having trouble getting [wpv-item index=split2] to display anything. Can I give you access to my site to check the code of the view?

I like how [wpv-item index=split2] will split each page in the middle so I can show an add.

Thanks,
Chuck

#599522

I will be around only after January.

I can assign this ticket here to Mohammed, he is back on Monday.

But if you copy paste the code I share above, it works fine.

If you can also confirm this, the issue you see is due to what you display - not how you display it.

It must then be an issue with the script, eventually.

Please let us know

#599524

It must be my script.

[wpv-layout-start]
  [wpv-post-body view_template="adsense-main-top"]
[wpv-items-found]
<!-- wpv-loop-start -->
	<wpv-loop wrap="2" pad="true">
		[wpv-item index=1]
		<div class="row fluid">
			<div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
          [wpv-item index=split2]
            [wpv-post-body view_template="adsense-main-middle"]
		[wpv-item index=other]
			<div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
		[wpv-item index=2]
			<div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
		</div>
		[wpv-item index=pad]
			<div class="col-sm-6"></div>
		[wpv-item index=pad-last]
			<div class="col-sm-6"></div>
		</div>
	</wpv-loop>
	<!-- wpv-loop-end -->
    
[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]
#600803

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Chuck

Beda is on vacation and this thread has only just been re-assigned to me, I'm sorry for the delay in getting back to you.

I tested this issue and it works with a simple unformatted Loop Output style, but doesn't with the Bootstrap grid style that you are using.

I tried several things to get it to work, such as changing the order of the index sections or hard-coding an index number such as 5 instead of using split2, but nothing was output in this case.

I've escalated this so that my colleagues can investigate further, I'll let you know when they have a fix.

In the meantime you could register a custom shortcode that checks the loop iteration and use that in a conditional check to show your advertising section after every, say, 6th post. (An even number because you are displaying posts in two columns.)

Here is the shortcode you would need to register:

/**
 * Add loop-iteration shortcode
 * 
 * Attributes
 * 'n' (optional) : test for nth iteration
 */
add_shortcode( 'loop-iteration', function( $atts ){

	global $loop_n;

	if ( !isset( $loop_n ) ) {
		$loop_n = 0;
		return $loop_n;
	}

	$loop_n++;

	if ( !isset( $atts['n'] ) ) {
		// no nth parameter, just return the current index
		return $loop_n;
	}
	else {
		$remainder = ( $loop_n + 1 ) % $atts['n'];
		return ( $remainder == 0 );
	}

});

You then need to go to Toolset > Settings > Front-end Content and register this loop-iteration shortcode under "Third-party shortcode arguments".

Then update your View to something like this:

[wpv-layout-start]
  [wpv-post-body view_template="adsense-main-top"]
[wpv-items-found]
<!-- wpv-loop-start -->
    <wpv-loop wrap="2" pad="true">

    [wpv-conditional if="( '[loop-iteration n='7']' eq '1' )"]
        </div>
      [wpv-post-body view_template="adsense-main-middle"]
        <div class="row">
    [/wpv-conditional]

        [wpv-item index=1]
        <div class="row fluid">
            <div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
        [wpv-item index=other]
            <div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
        [wpv-item index=2]
            <div class="col-sm-6">[wpv-post-body view_template="loop-item-in-main-posts-view"]</div>
        </div>
        [wpv-item index=pad]
            <div class="col-sm-6"></div>
        [wpv-item index=pad-last]
            <div class="col-sm-6"></div>
        </div>
    </wpv-loop>
    <!-- wpv-loop-end -->
     
[/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
    [/wpv-no-items-found]
[wpv-layout-end]
#601484

Thanks Nigel. No rush, busy time of year for everyone.

Is it best to insert the code you provided or wait until the escalation is resolved? The site is still under development, so I don't mind waiting until an answer is provided and only changing the code once.

Thank you,
Chuck

#601758

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Chuck

My colleague thinks this works but I can't get it to, so I'm waiting for clarification from him before I give you any further advice.

I'll be in touch.

#604603

Hi Nigel,

Any updates here?

Thanks,
Chuck

#604678

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

No news, I'm afraid.

It is with the developers but there is no fix proposed yet.

I've added a comment to the internal ticket to say that you are asking about progress.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.