• Hi guys,

    I thought this would be simple somehow but I guess it isn’t. Let me start by saying that I am no coder but willing to learn a bit if necessary. I was looking for a way to inject one (or more) widgetized area(s) into the wordpress loop, for instance before the 1st or 3rd etc post.

    Now I have found a few instructions for that but mostly it consists of registering a dynamic sidebar in functions.php and then hack index.php or category.php etc by looking for “endwhile” and inserting the dynamic sidebar before that.

    However, this is very theme dependent. Whenever I switch themes I need to adapt and reinsert that code to fit with the new theme, some of them don’t even seem to have that “endwhile” in index.php.

    So I was thinking of implementing this as a plugin, independently of the theme used. I have found a way to register the dynamic sidebar by creating a plugin consisting of a site_functions.php and adding this:

    register_sidebar(array('name'=> 'WidgetArea1',
        'id' => 'Widget1',
        'description' => esc_html__('Widgets appear before 1st post', ''),
        'before_widget' => '<section id="%1$s" class="WidgetArea1">',
        'after_widget' => '</section>',
        'before_title' => '',
        'after_title' => ''
    ));

    So now I know I am supposed to add something like this to the loop:

    <?php
    if ($count==1) {
    dynamic_sidebar(‘WidgetArea1’);
    }
    $count = $count + 1;
    ?>

    How and where can I do this (or something else ?) with my plugin so that the widget area appears in every theme, no matter how it displays the blog posts and which layout it uses (standard, masonry, isotope, etc) and without hacking theme files every time I switch themes.

    Ideally that would also be extendable to archive or category pages etc

    I am actually amazed that there doesn’t seem to be a plugin on the market that can do things like that already. There are thousands of plugins available for wordpress but obviously not for something apparently simple like that. I hope it is possible ?!?

    Thanks a lot for your help guys!
    Alex

    PS: If this is really complicated and any of you talented developers out there would prefer to create a plugin that does things like that I would even pay a little. Probably others as well … 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    This can be achived using a action hook.

    At the start of the main loop (while(have_posts()) and so on) the action called loop_start is fired. So you could do this.

    add_action('loop_start', 'my_awesome_loop_start');
    
    function my_awesome_loop_start() {
    	dynamic_sidebar(‘WidgetArea1’);
    }

    This would apply to every single loop start, so it would also work on archives.

    Does that answer your question?

    Thread Starter worldtravelgeek

    (@worldtravelgeek)

    Hi tormorten,

    thanks a lot for helping out. It works … somehow 😉

    The most basic version works:

    register_sidebar(array('name'=> 'WidgetAreaBeforePosts1',
        'id' => 'WidgetAreaBeforePosts1',
        'description' => esc_html__('WidgetArea that appears before 1st Post.')
    ));
    
    add_action('loop_start', 'include_WidgetAreaBeforePosts1');
    function include_WidgetAreaBeforePosts1() {
       dynamic_sidebar( 'WidgetAreaBeforePosts1' );
    }

    It will show an area before the first post but not as part of the post grid but above the grid so it is not part of the styling of the grid and will cover all colums of the grid (in my case the post grid has 2 colums)

    But anyway, at least it does show something.

    However, when I get a bit more advanced and try to show the widget area before 2nd or 3rd post by changing code like this, it wont work anymore:

    add_action('loop_start', 'include_WidgetAreaBeforePosts1');
    function include_WidgetAreaBeforePosts1() {
    	   static $box_count = 0;
    			if (++$box_count == 2) {
       dynamic_sidebar( 'WidgetAreaBeforePosts1' );
    
       }
    }

    Also, it does not work if I test for widgets in the widget areas:

    add_action('loop_start', 'include_WidgetAreaBeforePosts1');
    function include_WidgetAreaBeforePosts1() {
     if ( is_active_sidebar( 'WidgetAreaBeforePosts1' ) ) {
    	   static $box_count = 0;
    			if (++$box_count == 2) {
       dynamic_sidebar( 'WidgetAreaBeforePosts1' );
           }
       }
    }

    Any ideas?

    This action hook only fires at the start of each loop (so just once). So if $box_count probably wouldn’t be higher than 1 at any given time. For instance the post runs on every iteration of the loop, so this one might do the trick.

    Thread Starter worldtravelgeek

    (@worldtravelgeek)

    Thanks for your answer!

    For instance the post runs on every iteration of the loop, so this one might do the trick.

    Can you elaborate please? I dont get it …

    I was referring to the action hook add_action('the_post', 'my_function') but the underscore was stripped. This is executed at the start each of the posts in the loop. This is where you could run your code. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Insert widgetized area into loop, independent of theme’ is closed to new replies.