The Loop

This page is part of the plugin documentation for the Date Pagination plugin.

Here you’ll find examples about how to use this plugin in your theme.

The loop with pre_get_posts

In this example we assume the date pagination is set to monthly with the pre_get_posts action in your (child) theme’s functions.php file.

The WordPress functions get_next_posts_link() and get_previous_posts_link() are used to display the pagination links.

In this example the plugin’s label functions are used to get the dates for the current, next and previous (paginated) page.

<?php if ( have_posts() ) : ?>
	
	<h2>
		<?php
		// Echo current date with format 'F Y' ( e.g. November 2010 ).
		echo 'Current Date: ';
		echo km_dp_get_current_date_label( 'F Y' );
		?>
	</h2>

	<!-- The Loop -->
	<?php while ( have_posts() ) : the_post(); ?>
		<!-- Your loop code here -->
	<?php endwhile; ?>
	<?php

	// default labels
	$next_label = 'Previous Month';
	$prev_label = 'Next Month';

	// Check if functions exists (plugin is activated)
	if ( function_exists( 'km_dp_get_next_date_label' ) ) {
		$next_label = km_dp_get_next_date_label( 'F Y' );
	}

	if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
		$prev_label = km_dp_get_previous_date_label( 'F Y' );
	}
	?>

	<!-- WordPress core pagination functions (see the Codex) -->
	<?php next_posts_link( $next_label ); ?>
	<?php previous_posts_link( $prev_label ); ?>
	
<?php else:  ?>
	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

The loop with a custom Query

This example shows you a custom query (WP_Query) and the loop. The date pagination is set to 'monthly'.

The WordPress pagination functions get_next_posts_link() and get_previous_posts_link() are used to display the pagination links.

The plugin’s label functions are used to get the dates for the current, next and previous (paginated) page.

<?php
// Get the paged variable and use it in the custom query.
// (see: http://codex.wordpress.org/Pagination ).
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// Example query arguments.
$args = array(

	// set the date pagination to 'monthly'
	'date_pagination_type' => 'monthly', // 'yearly', 'monthly', 'daily'
	'paged'                => $paged,

	// Add your own WP_Query arguments here
	'post_type'            => array( 'post' ),
	'ignore_sticky_posts'  => true,
);

// The custom query.
$the_query = new WP_Query( $args );

// Variable $the_query is the query object.
// Don't forget to add the query object to the label functions when using a custom query;
?>

<?php if ( $the_query->have_posts() ) : ?>

	<h2>
		<?php
		// Echo current date with format 'F Y' ( e.g. November 2010 ).

		echo 'Current Date: ';
		echo km_dp_get_current_date_label('F Y', $the_query);
		?>
	</h2>

	<!-- The custom query Loop -->
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

		<!-- Your theme's loop code here -->
	<?php endwhile; ?>

	<?php
	// Date Pagination.

	// Default labels.
	$next_label = 'Previous Page';
	$prev_label = 'Next Page';	

	// Check if functions exists (plugin is activated).
	if ( function_exists( 'km_dp_get_next_date_label' ) ) {
		$next_label = km_dp_get_next_date_label( 'F Y', $the_query );
	}	

	if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
		$prev_label = km_dp_get_previous_date_label( 'F Y', $the_query );
	}
	?>

	<!-- WordPress core pagination functions (see the Codex) -->
	<?php
	// Set max_num_pages for next_posts_link() when using a custom query (see the Codex).
	// Get the max_num_pages from the custom query object ($the_query)
	next_posts_link( 'Previous month: ' . $next_label, $the_query->max_num_pages );
	previous_posts_link( 'Next month: ' . $prev_label );
	?>

	<?php wp_reset_postdata(); ?>
<?php else:  ?>
	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Advertisements