Move pagination component to function

This commit is contained in:
Holger Könemann 2016-11-23 10:42:53 +01:00
parent bbbfbae63b
commit c5975e5f11
9 changed files with 138 additions and 100 deletions

View File

@ -53,9 +53,6 @@ $sidebar_pos = get_theme_mod( 'understrap_sidebar_position' );
<?php endwhile; ?> <?php endwhile; ?>
<!-- the pagination component -->
<?php require get_template_directory() . '/global-templates/pagination.php';understrap_pagination();?>
<?php else : ?> <?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?> <?php get_template_part( 'loop-templates/content', 'none' ); ?>
@ -64,6 +61,9 @@ $sidebar_pos = get_theme_mod( 'understrap_sidebar_position' );
</main><!-- #main --> </main><!-- #main -->
<!-- The pagination component -->
<?php understrap_pagination(); ?>
</div><!-- #primary --> </div><!-- #primary -->
<!-- Do the right sidebar check --> <!-- Do the right sidebar check -->

View File

@ -70,9 +70,6 @@ $sidebar_pos = get_theme_mod( 'understrap_sidebar_position' );
</li> </li>
<?php endwhile; ?> <?php endwhile; ?>
<!-- the pagination component -->
<?php require get_template_directory() . '/global-templates/pagination.php';understrap_pagination();?>
<?php else : ?> <?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?> <?php get_template_part( 'loop-templates/content', 'none' ); ?>
@ -85,6 +82,9 @@ $sidebar_pos = get_theme_mod( 'understrap_sidebar_position' );
</main><!-- #main --> </main><!-- #main -->
<!-- The pagination component -->
<?php understrap_pagination(); ?>
</div><!-- #primary --> </div><!-- #primary -->
<!-- Do the right sidebar check --> <!-- Do the right sidebar check -->

View File

@ -32,6 +32,11 @@ require get_template_directory() . '/inc/enqueue.php';
*/ */
require get_template_directory() . '/inc/template-tags.php'; require get_template_directory() . '/inc/template-tags.php';
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/inc/pagination.php';
/** /**
* Custom functions that act independently of the theme templates. * Custom functions that act independently of the theme templates.
*/ */

View File

@ -69,6 +69,8 @@ if ( is_front_page() && is_home() ) {
<?php if ( 'masonry' === $posts_style ) : ?></div><?php endif; ?> <?php if ( 'masonry' === $posts_style ) : ?></div><?php endif; ?>
</main><!-- #main --> </main><!-- #main -->
<?php understrap_pagination(); ?>
</div><!-- #primary --> </div><!-- #primary -->
<!-- Do the right sidebar check --> <!-- Do the right sidebar check -->
@ -79,9 +81,7 @@ if ( is_front_page() && is_home() ) {
<?php endif; ?> <?php endif; ?>
</div><!-- .row --> </div><!-- .row -->
<?php require get_template_directory() . '/global-templates/pagination.php';
understrap_pagination();
?>
</div><!-- Container end --> </div><!-- Container end -->
</div><!-- Wrapper end --> </div><!-- Wrapper end -->

95
inc/pagination.php Normal file
View File

@ -0,0 +1,95 @@
<?php
/**
* Pagination layout.
*
* @package understrap
*/
// Comments form.
/**
* Custom Pagination with numbers
* Credits to http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
*/
if ( ! function_exists( 'understrap_pagination' ) ) :
function understrap_pagination() {
if ( is_singular() ) {
return;
}
global $wp_query;
/** Stop execution if there's only 1 page */
if ( $wp_query->max_num_pages <= 1 ) {
return;
}
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 ) {
$links[] = $paged;
}
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<nav aria-label="Page navigation"><ul class="pagination ">' . "\n";
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active page-item"' : ' class="page-item"';
printf( '<li %s><a class="page-link" href="%s"><i class="fa fa-step-backward" aria-hidden="true"></i></a></li>' . "\n",
$class, esc_url( get_pagenum_link( 1 ) ), '1' );
/** Previous Post Link */
if ( get_previous_posts_link() ) {
printf( '<li class="page-item"><span class="page-link">%1$s</span></li> ' . "\n",
get_previous_posts_link( '<span aria-hidden="true">&laquo;</span><span class="sr-only">Previous page</span>' ) );
}
if ( ! in_array( 2, $links ) ) {
echo '<li class="page-item"></li>';
}
}
// Link to current page, plus 2 pages in either direction if necessary.
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active page-item"' : ' class="page-item"';
printf( '<li %s><a href="%s" class="page-link">%s</a></li>' . "\n", $class,
esc_url( get_pagenum_link( $link ) ), $link );
}
// Next Post Link.
if ( get_next_posts_link() ) {
printf( '<li class="page-item"><span class="page-link">%s</span></li>' . "\n",
get_next_posts_link( '<span aria-hidden="true">&raquo;</span><span class="sr-only">Next page</span>' ) );
}
// Link to last page, plus ellipses if necessary.
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) ) {
echo '<li class="page-item"></li>' . "\n";
}
$class = $paged == $max ? ' class="active "' : ' class="page-item"';
printf( '<li %s><a class="page-link" href="%s" aria-label="Next"><span aria-hidden="true"><i class="fa fa-step-forward" aria-hidden="true"></i></span><span class="sr-only">%s</span></a></li>' . "\n",
$class . ' page-item 9', esc_url( get_pagenum_link( esc_html( $max ) ) ), esc_html($max ) );
}
echo '</ul></nav>' . "\n";
}
endif;

View File

@ -137,89 +137,27 @@ if ( ! function_exists( 'understrap_post_nav' ) ) :
return; return;
} }
?> ?>
<nav class="navigation post-navigation">
<h1 class="sr-only"><?php _e( 'Post navigation', 'understrap' ); ?></h1>
<div class="nav-links">
<?php
if ( get_previous_post_link() ) { <div class="row">
previous_post_link( '<span class="nav-previous float-xs-left btn btn-sm btn-secondary">%link</span>', _x( '<span class="fa fa-backward"></span>&nbsp;%title', 'Previous post link', 'understrap' ) ); <div class="col-md-12">
} <nav class="navigation post-navigation">
if ( get_next_post_link() ) { <h1 class="sr-only"><?php _e( 'Post navigation', 'understrap' ); ?></h1>
next_post_link( '<span class="nav-next float-xs-right btn btn-sm btn-secondary">%link</span>', _x( '%title&nbsp;<span class="fa fa-forward"></span>', 'Next post link', 'understrap' ) ); <div class="nav-links">
} <?php
?>
</div><!-- .nav-links --> if ( get_previous_post_link() ) {
</nav><!-- .navigation --> previous_post_link( '<button class="nav-previous float-xs-left btn btn-sm btn-secondary">%link</button>', _x( '<i class="fa fa-angle-left" aria-hidden="true"></i>
&nbsp;%title', 'Previous post link', 'understrap' ) );
}
if ( get_next_post_link() ) {
next_post_link( '<button class="nav-next float-xs-right btn btn-sm btn-secondary">%link</button>', _x( '%title&nbsp;<i class="fa fa-angle-right" aria-hidden="true"></i>
</span>', 'Next post link', 'understrap' ) );
}
?>
</div><!-- .nav-links -->
</nav><!-- .navigation -->
</div>
</div>
<?php <?php
} }
endif;
/**
* Custom Pagination with numbers
* Credits to http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
*/
if ( ! function_exists( 'understrap_numeric_posts_nav' ) ) :
function understrap_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<nav aria-label="Page navigation"><ul class="pagination">' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() ) {
//printf( '<button class="nav-previous btn btn-ghost btn-raised"><span class="fa fa-backward"></span> %s</button>' . "\n", get_previous_posts_link() );
?>
<li class="page-item">
<?php previous_posts_link( '<span class="fa fa-backward"></span> '. __('Newer posts', 'understrap' )); ?>
</li>
<?php
}
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? 'active' : '';
printf( '<li class="page-item"><a class="page-link" href="%s">%s</a></span>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li class="page-item">...</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? 'active' : '';
printf( '<li class="page-item" %s"><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li class="page-item">...</li>' . "\n";
$class = $paged == $max ? ' active' : '';
printf( '<li class="page-item %s"><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() ) {
//printf( '<button class="nav-next btn btn-ghost btn-raised pull-right">%s <span class="fa fa-forward"></span></button>' . "\n", get_next_posts_link() );
?>
<li class="page-item">
<?php next_posts_link( __('Older posts', 'understrap' ) . ' <span class="fa fa-forward"></span>'); ?>
</li>
<?php
}
echo '</ul></nav>' . "\n";
}
endif; endif;

View File

@ -52,9 +52,6 @@ if ( is_front_page() && is_home() ) {
<?php endwhile; ?> <?php endwhile; ?>
<!-- the pagination component -->
<?php require get_template_directory() . '/global-templates/pagination.php';understrap_pagination();?>
<?php else : ?> <?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?> <?php get_template_part( 'loop-templates/content', 'none' ); ?>
@ -63,6 +60,9 @@ if ( is_front_page() && is_home() ) {
</main><!-- #main --> </main><!-- #main -->
<!-- The pagination component -->
<?php understrap_pagination(); ?>
</div><!-- #primary --> </div><!-- #primary -->
<!-- Do the right sidebar check --> <!-- Do the right sidebar check -->

View File

@ -132,9 +132,9 @@ a.skip-link {
.post-navigation, .post-navigation,
.paged-navigation { .paged-navigation {
padding-top: 1rem; padding: ($grid-gutter-width-base/2) 0;
margin-top: 1rem; margin: $grid-gutter-width-base 0;
border-top: 1px solid $gray-dark; border-top: 1px solid $gray-lighter;
} }
// Fixing BS dropdown in a dropdown // Fixing BS dropdown in a dropdown

View File

@ -45,9 +45,6 @@ $sidebar_pos = get_theme_mod( 'understrap_sidebar_position' );
<?php endwhile; ?> <?php endwhile; ?>
<!-- the pagination component -->
<?php require get_template_directory() . '/global-templates/pagination.php';understrap_pagination();?>
<?php else : ?> <?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?> <?php get_template_part( 'loop-templates/content', 'none' ); ?>
@ -56,6 +53,9 @@ $sidebar_pos = get_theme_mod( 'understrap_sidebar_position' );
</main><!-- #main --> </main><!-- #main -->
<!-- The pagination component -->
<?php understrap_pagination(); ?>
</div><!-- #primary --> </div><!-- #primary -->
<!-- Do the right sidebar check --> <!-- Do the right sidebar check -->