Add Banner After Any Number of Posts On Any Archive Page In Twenty Fifteen

This code creates a custom archive file you can add to your child theme if you want to add a banner or custom content after any number of excerpts.

after-post-archive

In this example it outputs after every 3rd post however you can simply change the 3 in the code to any other number:

<?php

get_header(); ?>

	<section id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

		<?php if ( have_posts() ) : ?>

			<header class="page-header">
				<?php
					the_archive_title( '<h1 class="page-title">', '</h1>' );
					the_archive_description( '<div class="taxonomy-description">', '</div>' );
				?>
			</header><!-- .page-header -->

			<?php $counter = 1;
			// Start the Loop.
			while ( have_posts() ) : the_post();

				get_template_part( 'content', get_post_format() );
				
				if( $counter === 3 ) { ?>
	
                <footer class="entry-footer">Your Custom Content After The 3rd Excerpt</footer>
               
                <?php } 
			// End the loop.
			 $counter++; 
			 endwhile;

			// Previous/next page navigation.
			the_posts_pagination( array(
				'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
				'next_text'          => __( 'Next page', 'twentyfifteen' ),
				'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
			) );

		// If no content, include the "No posts found" template.
		else :
			get_template_part( 'content', 'none' );

		endif;
		?>

		</main><!-- .site-main -->
	</section><!-- .content-area -->

<?php get_footer(); ?>

Using a code editor, create a new file named archive.php, category.php or anything else according to the WordPress template hierarchy for archives pages and upload to your child themes root directory.

Here’s the code in the above file which you can use to display your banner or custom content:

<footer class="entry-footer">Your Custom Content After The 3rd Excerpt</footer>

Alternative Option – Add New Widget Area

Rather than hard code your custom content directly into a archive file, you can register a new widget area in your child themes functions file and call it in your archive file.

(1) Add the following PHP code to your child themes functions file:

function twentyfifteen_custom_sidebar() {
	register_sidebar( array(
		'name'          => __( 'Custom Widget', 'twentyfifteen' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Add widgets here to appear after any number of posts on archive pages.', 'twentyfifteen' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'twentyfifteen_custom_sidebar' );

(2) Use the following code to created a custom archive.php file in your child theme. This example executes the widget content after the 2nd post on all archive pages:

<?php
/**
 * The template for displaying archive pages
 *
 * Used to display archive-type pages if nothing more specific matches a query.
 * For example, puts together date-based pages if no date.php file exists.
 *
 * If you'd like to further customize these archive views, you may create a
 * new template file for each one. For example, tag.php (Tag archives),
 * category.php (Category archives), author.php (Author archives), etc.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 * @author Brad Dalton
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */

get_header(); ?>

	<section id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

		<?php if ( have_posts() ) : ?>

			<header class="page-header">
				<?php
					the_archive_title( '<h1 class="page-title">', '</h1>' );
					the_archive_description( '<div class="taxonomy-description">', '</div>' );
				?>
			</header><!-- .page-header -->

			<?php $counter = 1;
			// Start the Loop.
			while ( have_posts() ) : the_post();

				get_template_part( 'content', get_post_format() );
				
				if( $counter === 2 ) { 
	         
	         	if ( is_active_sidebar( 'sidebar-2' ) ) : ?>
			    <div class="widget-area" role="complementary">
				<?php dynamic_sidebar( 'sidebar-2' ); ?>
			    </div><!-- .widget-area -->
		        <?php endif; 
             } 
			// End the loop.
			 $counter++; 
			 endwhile;

			// Previous/next page navigation.
			the_posts_pagination( array(
				'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
				'next_text'          => __( 'Next page', 'twentyfifteen' ),
				'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
			) );

		// If no content, include the "No posts found" template.
		else :
			get_template_part( 'content', 'none' );

		endif;
		?>

		</main><!-- .site-main -->
	</section><!-- .content-area -->

<?php get_footer(); ?>

Related Tutorials

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.