• Resolved JackGraal

    (@jackgraal)


    What can I do to split posts on home page into two or more blocks and add something in between?

    For example: first 4 posts, then something like a widget, then 4 next posts, then another widget and then the last 2 posts.

Viewing 11 replies - 16 through 26 (of 26 total)
  • Sorry to revive this old thread.
    I would like to have a similar setup:

    widget 1
    Featured post
    post | post
    post | post
    widget 2
    post | post
    post | post
    widget 2
    post | post
    post | post

    Is that possible?
    What should I change in bdbrown’s code above to get this?

    Thanks in advance

    This post describes basically the same process but uses dynamic sidebars (which are already in the theme) instead of a shortcode. You would just change

    if ($i % 6== 0)

    to

    if ($i % 4== 0)

    To show an ad before the featured image place a dynamic sidebar above that line in index.php:

    <div class="pad group">
    	echo '<div class="my-ad-widget">';		// create the widget div
    	dynamic_sidebar('sidebar-ads');		// show the widget
    	echo '</div>'; 				 	// close the widget div
    	<?php get_template_part('inc/featured'); ?>

    If I wanted to do this without the use of dynamic sidebars, would I still use the code in the last part of your reply, or is it a differnt code for that?

    Just replace the dynamic sidebar line with whatever you want to display at that point in the page.

    It is not working properly (because I’m definitely doing something wrong here).

    If I follow the instructions in this post, it all goes fine: the widget appears as follows:

    Featured post
    post | post
    post | post
    post | post
    widget

    But after I move the code you mentioned last in your post above it doesn’t work properly. Here’s what I see in the site:

    https://oi58.tinypic.com/1jb8nk.jpg

    The widget reappears correctly like before

    xxxxxx <<<<-error
    Featured post
    post | post
    post | post
    post | post
    widget <<<<<-here

    What am I doing wrong?

    Here’s the code, if it may be of any help

    <?php get_header(); ?>
    
    <section class="content">
    
    	<?php get_template_part('inc/page-title'); ?>
    
    	<div class="pad group">
    	echo '<div class="my-ad-widget">';		// create the widget div
    	dynamic_sidebar('sidebar-ads1');		// show the widget
    	echo '</div>'; 				 	// close the widget div
    		<?php get_template_part('inc/featured'); ?>
    
    		<?php if ( have_posts() ) : ?>
    
    			<?php if ( ot_get_option('blog-standard') == 'on' ): ?>
    				<?php while ( have_posts() ): the_post(); ?>
    					<?php get_template_part('content-standard'); ?>
    				<?php endwhile; ?>
    			<?php else: ?>
    <div class="post-list group">
    
    <?php $i = 1;								// initialize the post counter
    echo '<div class="post-row">';				// create the post-row div
    while ( have_posts() ): the_post(); ?>		<!-- while we have posts -->
    	<?php get_template_part('content'); ?>		<!-- display the post -->
    
    	<?php if ($i % 2 == 0) {				// if we've shown 2 posts
    		echo '</div>';						// close this post-row div
    
    		// BEGIN insert my custom widget after 6 posts
    		if ($i % 6== 0) {					// if we've displayed 6 posts
    			echo '<div class="my-ad-widget">';		// create the widget div
    			dynamic_sidebar('sidebar-ads1');		// show the widget
    			echo '</div>'; 				 	// close the widget div
    		}
    		// END insert my custom widget
    
        echo '<div class="post-row">';			// create the next post-row div
    	}
    
    	$i++;									// increment the post counter
    endwhile;
    echo '</div>';								// close the trailing post-row div
    ?>
    
    </div><!--/.post-list-->
    			<?php endif; ?>
    
    			<?php get_template_part('inc/pagination'); ?>
    
    		<?php endif; ?>
    
    	</div><!--/.pad-->
    
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    I think I found (part of) what’s wrong.

    The code needs to be in php tags

    <div class="pad group">
    		<?php
    			echo '<div class="my-ad-widget">';		// create the widget div
    			dynamic_sidebar('sidebar-ads1');		// show the widget
    			echo '</div>'; 				 	// close the widget div ?>
    		<?php get_template_part('inc/featured'); ?>

    Now the widget appears, but it also shows a massive gap between the bottom of the widget and the first two regular posts, and it hides the featured post completely.

    Almost there…

    Found it!

    I removed the CSS entry completely

    .my-ad-widget {
      float:left;
    }

    It now works perfectly.

    Thank you so much!

    Yep, sorry about that. Missed the php tags on the block above the featured line. Glad you got it working.

    Hello JackGraal and bdbrown

    How could use this solution in my loop?
    my loop this at this link: https://bolafurada.com.br/loop.txt

    If you can help me, thank you very much

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

Viewing 11 replies - 16 through 26 (of 26 total)
  • The topic ‘Split loop and add a section between posts on home page’ is closed to new replies.