• Resolved veetoe

    (@veetoe)


    Well I’m pretty new at this, but I managed to make the widget area between the posts, but the problem is that the widget appears after EVERY post of my blog (i.e, I have 5 posts and it creates a widget for each post — 5 total widget areas) How do I make just one widget area between my blog posts? (i.e, post–post–widget area–post–post–post–etc)

    I understand I have to use a $loop_counter but where do I put this, or if not then what should I do?

    /** Register Widget Box widget area */
    genesis_register_sidebar( array(
    ‘id’ => ‘widget-box’,
    ‘name’ => __( ‘Widget Box’, ‘genesis’ ),
    ‘description’ => __( ‘This is the widget box’, ‘genesis’ ),
    ) );

    /** Add Widget Box between posts on homepage */
    add_action( ‘genesis_after_post’, ‘include_widget_box’ );
    function include_widget_box() {
    if ( is_home() && is_active_sidebar( ‘widget-box’ ) ) {
    echo ‘<div class=”widget-box”>’;
    dynamic_sidebar( ‘widget-box’ );
    echo ‘</div><!– end .widget-box –>’;
    }}

    [ Please do not bump, that’s not permitted here. ]

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can add a static variable counter to your include_widget_box() function. This is untested, but it should be close:

    function include_widget_box() {
       static $box_count = 0;
       if ( is_home() && is_active_sidebar( 'widget-box' ) ) {
          if (++$box_count == 1) {
             echo '<div class="widget-box">';
             dynamic_sidebar( 'widget-box' );
             echo '</div><!-- end .widget-box -->';
          }
       }
    }
    Thread Starter veetoe

    (@veetoe)

    That works. The widget appears between the posts without repeating itself.

    However, when I navigate older posts (i.e, click on the navigation link to view older blog posts) the widget appears again on every new page. I would like the widget to only appear on the first homepage. Is there something missing I should add to the code to do that?

    You need to add a test for the page number:

    Try changing this:

    if ( is_home() && is_active_sidebar( 'widget-box' ) ) {
       if (++$box_count == 1) {

    to this:

    if ( is_home() && is_active_sidebar( 'widget-box' ) ) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       if (++$box_count == 1 && $paged == 1) {
    Thread Starter veetoe

    (@veetoe)

    That solves it, you’ve made this too easy. Thanks for teaching me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add a widget area between posts?’ is closed to new replies.