Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need Content Between Posts WordPress

  • SOLVED

So, Genesis takes a little way of adding ad spots or widgets between so many posts.... not too much different from the old tutorial for most other WordPress frameworks can use without a hook. Code that I'm using in Genesis for is:

// Put text into the loop but only after every third post
add_action( 'genesis_after_entry', 'add_text_after_post_entry' );

function add_text_after_post_entry() {
global $loop_counter;

$loop_counter++;

if( $loop_counter == 4 ) {

echo 'AD CODE or REPLACE THIS LINE WITH CODE TO CALL A WIDGET';

$loop_counter = 0;

}

}


http://webhost-solutions.net/diyp/ is the site.

I'm having a dilemma though. Using the counter, and registering widgets, and considering there are 10 posts listed on a page, For 1st post, they get widget 1, 2nd post gets widget 2, and up to 10th which gets widget 10. And because they are widgets, if it's filled, it's filled, if it's not... who cares.

Is this possible to do? I've not tried it, and trying to wrap my head around it. Any ideas?

EDIT:

So the idea is:

is post 1 is, then produce widget 1, if post 2 is, then produce widget 2, and so on, up to 10. I don't need the code for registering widgets or the widget. I need for the counter to be able to do this.

EDIT #2:

Prize will be edited to $30 on Friday for whomever gets this. I appreciate the attempts, but Sebastien has nearly hit it with one small tweak left that will hit this on the head.

EDIT #3:

The issue was half resolved... I added $5. It's only logical with this loop that you're not going to want it on single posts and add a conditional statement with !is_single().

Answers (3)

2014-02-13

Sébastien | French WordpressDesigner answers:

could you confirm that you have one sidebar for each of your 10 widgets ?




use this code :

add_action( 'genesis_after_entry', 'add_text_after_post_entry' );
function add_text_after_post_entry() {
//we can use for example an array of IDs widgets
$slh_widgets_ids = array('first-post','second-post','third-post','post4','post5','post6','post7','post8','post9','post10');

global $post_count;
$post_count++;

$slh_widget_id = $slh_widgets_ids[$post_count-1];

if( $post_count < 11 ) {
echo '<div class="'.$slh_widget_id.'">';
dynamic_sidebar( $slh_widget_id );
echo '</div><!-- end .'.$slh_widget_id.' -->';
}
}


you must customize this line :
$slh_widgets_ids = array('first-post','second-post','third-post','post4','post5','post6','post7','post8','post9','post10');
with the IDs of your dynamic sidebars.

If your dynamic sidebars names are : sidebar1, sidebar2, sidebar3, sidebar4 etc...
you can use this code :

add_action( 'genesis_after_entry', 'add_text_after_post_entry' );
function add_text_after_post_entry() {
global $post_count;
$post_count++;

if( $post_count < 11 ) {
echo '<div class="sidebar'.$post_count.'">';
dynamic_sidebar( 'sidebar'.$post_count );
echo '</div><!-- end .sidebar'.$post_count.' -->';
}
}

If you want use this code for 150posts instaed of 10 posts, change simply the line
if( $post_count < 11 ) {
into
if( $post_count < 151 ) {


Nile Flores comments:

Yes, it is 10... I will go and try this one.


Nile Flores comments:

Sebastien - This is almost perfect. I had to install Widget logic and give an single post after-entry widget a is_single() conditional tag because it was showing up in the main feed. Any reason why this was occurring, or how to fix this without having to use the plugin. Site is - [[LINK href="http://webhost-solutions.net/diyp/"]]http://webhost-solutions.net/diyp/[[/LINK]] and I've got a status call with my client tonight after midnight (CST USA), but afterwards, I'll take the conditional tag down so you can see what's occurring in case you're not certain what's going on.

This is nearly what I need, but in the case I need to apply it for a future project, we need this not to throw back the output of another widget that's suppose to be in the single post. Thanks!


Nile Flores comments:

The other issue I noted to this code was that it needs to only show up when the widget is filled as well. So, if it's not there.


Example



if ( is_active_sidebar( 'nameofsidebar' )) {
echo '<div class="nameofsidebar widget-area"><div class="wrap">';
dynamic_sidebar( 'nameofsidebar' );
echo '</div></div><!-- end .nameofsidebar -->';
}



Nile Flores comments:

single posts also have that widget too.... so it's not suppose to be single for the sidebar. If you click on the end of the post, after the comments, this is outputting in single posts as well.


Sébastien | French WordpressDesigner comments:

If you want to verify a sidebar is active, the code is :

add_action( 'genesis_after_entry', 'add_text_after_post_entry' );
function add_text_after_post_entry() {
//we can use for example an array of IDs widgets
$slh_widgets_ids = array('first-post','second-post','third-post','post4','post5','post6','post7','post8','post9','post10');
global $post_count;
$post_count++;
$slh_widget_id = $slh_widgets_ids[$post_count-1];

if( $post_count < 11 ) {
if(is_active_sidebar($slh_widget_id)) {
echo '<div class="'.$slh_widget_id.'">';
dynamic_sidebar( $slh_widget_id );
echo '</div><!-- end .'.$slh_widget_id.' -->';
}
}
}


Not sur to understand all your comments. Do you have an another question ?

I note that you have write "I will be increasing my reward (to $20 or $30) if someone can come up with the answer that works..."
It's cool :-))

2014-02-13

Bob answers:

You might need to create 10 sidebars and call them using get_sidebar() function.
[[LINK href="http://codex.wordpress.org/Function_Reference/get_sidebar"]]http://codex.wordpress.org/Function_Reference/get_sidebar[[/LINK]]

or

You can create 10 textarea in your theme then call ad code using <strong>get_option('id_of_ad')</strong>


Bob comments:

Do you want something like this?

if ad code is available for counter 1 then it will be displayed after first post and if it is available at 5 then it will be displayed after 5th post.

or you just want to add code 3 times after 3rd, 6th abd 9th post?

You can also use <strong>dynamic_sidebar</strong> if you wish.

[[LINK href="http://codex.wordpress.org/Function_Reference/dynamic_sidebar"]]http://codex.wordpress.org/Function_Reference/dynamic_sidebar[[/LINK]]


Nile Flores comments:

I don't need that... I've already registered the sidebars.

What I need is the code above altered to coordinate with that loop statement so each of those sidebars (that I already created in my functions.php file) work.


Nile Flores comments:

Bhavesh Vaghela reply to your 02/13/14 4:21am response:

What I mean is:

if ad code is available for counter 1 then it will be displayed after first post

AND

if ad code is available for counter 2 then it will be displayed after second post

AND

if ad code is available for counter 3 then it will be displayed after third post

AND

if ad code is available for counter 4 then it will be displayed after fourth post

AND

if ad code is available for counter 5 then it will be displayed after fifth post

AND

if ad code is available for counter 6 then it will be displayed after sixth post

AND

if ad code is available for counter 7 then it will be displayed after seventh post

AND

if ad code is available for counter 8 then it will be displayed after eighth post

AND

if ad code is available for counter 9 then it will be displayed after ninth post

AND

if ad code is available for counter 10 then it will be displayed after tenth post

This is because the site displays 10 posts to a page.

----------
Note: I will be increasing my reward (to $20 or $30) if someone can come up with the answer that works... not giving me tutorials to other places.


Bob comments:

You want to alter above code so here it is.

Here is code.

I suppose that your sidebar name are as sidebar-1, sidebar-2, sidebar-3.....

notice <strong>if condition</strong>. It will check that if you sidebar contain any widget or not.
If there is widget then it will display sidebar.

add_action( 'genesis_after_entry', 'add_text_after_post_entry' );
function add_text_after_post_entry() {
global $loop_counter; //
if( is_active_sidebar( 'sidebar-'.$loop_counter ) ){ //check if sidebar is active
dynamic_sidebar( 'sidebar-'.$loop_counter ); // then display it
}
$loop_counter++;
}


Bob comments:

I am not sure but I think $loop_counter is genesis global variable.
If so and it is increased by genesis itself each time after <em>genesis_after_entry</em> hook then we do not need to increase it.
so remove it.

If above code do not work properly then try below one.

add_action( 'genesis_after_entry', 'add_text_after_post_entry' );
function add_text_after_post_entry() {
global $loop_counter;
if( is_active_sidebar( 'sidebar-'.$loop_counter ) ){ //check if sidebar is active
dynamic_sidebar( 'sidebar-'.$loop_counter ); // then display it
}
}


Nile Flores comments:

Genesis is HTML5 now and that doesn't work like it use to.... unfortunately.

2014-02-13

Deepak answers:

Nile Flores !Try this links may this help to you

http://wordpress.org/support/topic/how-to-add-content-between-posts
http://codex.wordpress.org/The_Loop_in_Action
http://www.htmlite.com/php015.php

Thanks!


Nile Flores comments:

Nope... I wrote the original tutorial on adding content between the posts using the loop counter several years ago. Now I just need this to work with all 10, and have it work using the loop counter.

I will be increasing my reward (to $20 or $30) if someone can come up with the answer that works... not giving me tutorials to other places.