• 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 15 replies - 1 through 15 (of 26 total)
  • bdbrown

    (@bdbrown)

    This article shows a way to insert code after the first post. You might be able to modify that for your purpose.

    Thread Starter JackGraal

    (@jackgraal)

    I tried but it doesn’t work. I don’t know where to put the code in the first place

    Here’s the index.php code for hueman theme:

    <?php get_header(); ?>
    
    <section class="content">
    
    	<?php get_template_part('inc/page-title'); ?>
    
    	<div class="pad group">
    
    		<?php get_template_part('inc/featured'); ?>
    
    		<?php if ( have_posts() ) : ?>
    
    			<div class="post-list group">
    				<?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ): the_post(); ?>
    				<?php get_template_part('content'); ?>
    				<?php if($i % 2 == 0) { echo '</div><div class="post-row">'; } $i++; endwhile; echo '</div>'; ?>
    
    			</div><!--/.post-list-->
    
    			<?php get_template_part('inc/pagination'); ?>
    
    		<?php endif; ?>
    
    	</div><!--/.pad-->
    
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Maybe there is a way to register a widget to show after X posts?

    bdbrown

    (@bdbrown)

    Let me see what I can come up with and I’ll get back to you.

    bdbrown

    (@bdbrown)

    Is it possible to post the code you’re trying to insert?

    Thread Starter JackGraal

    (@jackgraal)

    I don’t have a ready code, but this topic is similar to what I want to achieve.

    I don’t know how to register the new widget area (in functions.php) and what code should I put into index.php

    bdbrown

    (@bdbrown)

    Hi Jack – I think I have a solution. I’ve tested it on my child theme and it works. For reference, here’s the starting point:
    The default theme style is to show 2 posts across. “The loop” in index.php does the following:
    if there are posts, continue
    create the post-list div
    initialize a post counter $i
    create the post-row div
    while there are posts to display
    display the next post
    if we’ve displayed 2 posts, close the post-row div and create a new one
    increment the post counter
    when no more posts
    close the last post-row div
    close the post-list div

    To show your widget after every 4 posts we need to:
    1. install the “Widget Shortcode” plugin
    2. modify index.php to insert the widget every 4 posts
    3. add css to style the widget

    Widget Shortcode Plugin
    Installing the plugin is straight-forward. It creates a widget id shortcode for every widget that’s used in a sidebar. That widget id is used in the index.php code. If you want to use a widget that’s not in one of your existing sidebars, and don’t want to add the widget to an existing sidebar, create a new sidebar in Theme Options – Sidebars and drop the widget in there.

    Modifying index.php
    In index.php, delete lines 13 – 17:

    <div class="post-list group">
    <?php $i = 1; echo '<div class="post-row">'; while ( have_posts() ): the_post(); ?>
    <?php get_template_part('content'); ?>
    <?php if($i % 2 == 0) { echo '</div><div class="post-row">'; } $i++; endwhile; echo '</div>'; ?>
    </div><!--/.post-list-->

    Replace that block of code with the following:

    <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 4 posts
    		if ($i % 4 == 0) {																						// if we've displayed 4 posts
    			echo '<div id="my-widget">';													// create the widget div
          echo do_shortcode ( '[widget id="text-3"]' );		   // 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-->

    The [widget id=”text-3″] should be replaced with your widget shortcode.

    Adding the Styles
    Place this in your css and add any other styles you need:

    #my-widget {
      float: left;
      }

    Let me know how it goes.

    Thread Starter JackGraal

    (@jackgraal)

    Yay, it works!

    Thank you ??

    Now I just have a question – how can I hide the widget areas on next pages? I would like to make it visible only on home page. The Visibility feature of wordpress widgets doesn’t seem to work.

    bdbrown

    (@bdbrown)

    Glad it’w working. You should be able to hide the widget with this css:

    body.paged #my-widget {
      display: none;
        }

    Thread Starter JackGraal

    (@jackgraal)

    Yeah, it’s working.

    Thank you for your awesome help!

    Thread Starter JackGraal

    (@jackgraal)

    Hi again, I’m still editing my theme and I’d like to add a widget between posts as you managed to achieve some time ago.

    But I’d like to add two different widgets – one after 3 posts and another one after 7 posts (the first post is a featured).

    How can I do it so the widgets will appear only once and only on the home page?

    So, if I understand what you’re after, right now you have this:

    Featured post
    post post
    post post
    widget
    post post
    post post
    widget

    And you want to change it to this?
    Featured post
    post post post
    widget 1
    post post post
    widget 2

    Thread Starter JackGraal

    (@jackgraal)

    I want two independent widgets and the home page to look like this:

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

    Try replacing this code block:

    // BEGIN insert my custom widget after 4 posts
    if ($i % 4 == 0) {					// if we've displayed 4 posts
    	echo '<div id="my-widget">';			// create the widget div
    	echo do_shortcode ( '[widget id="text-3"]' );	// show the widget
    	echo '</div>';					// close the widget div
    }
    // END insert my custom widget

    With this:

    // BEGIN insert my custom widgets
    if ($i == 2) {						// if we've displayed 2 posts
    	echo '<div id="my-widget">';			// create the widget div
    	echo do_shortcode ( '[widget id="text-1"]' );	// show the widget
    	echo '</div>';					// close the widget div
    } elseif ($i == 6) {					// if we've displayed 6 posts
    	echo '<div id="my-widget">';			// create the widget div
    	echo do_shortcode ( '[widget id="text-2"]' );	// show the widget
    	echo '</div>';					// close the widget div
    }
    // END insert my custom widgets
    Thread Starter JackGraal

    (@jackgraal)

    Yeah, it works just the way I wanted. Awesome!

    Thank you ??

    Thank you so much for your code, But I’am so sorry it have error ??
    The widget shortcode seem doesn’t work, and I don’t know how to fix it. Please help me

    https://chuyenhoc.com/

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