php 将分页添加到页面模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将分页添加到页面模板相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Template Name: Archive Page with bootstrap */
?>

<?php
	$paged = (int) get_query_var('paged');
	$args = array(
		'posts_per_page' => 5,
		'paged' => $paged,
		'orderby' => 'post_date',
		'order' => 'DESC',
		'post_type' => 'post',
		'post_status' => 'publish'
	);
	$the_query = new WP_Query($args);

	if ( $the_query->have_posts() ) :

		while ( $the_query->have_posts() ) : $the_query->the_post();
			get_template_part( 'template-parts/content', get_post_format() );
		endwhile;

	else:
		get_template_part( 'template-parts/content', 'none' );
	endif;

	if ($the_query->max_num_pages > 1) {

		$args = array(
			'base' => get_pagenum_link(1) . '%_%',
			'format' => 'page/%#%/',
			'current' => max(1, $paged),
			'total' => $the_query->max_num_pages
		);

		$navigation = '';

		$links = paginate_links( $args );
		if ( $links ) {

			$links = preg_replace("/&laquo; Previous/", "<i class='fa fa-angle-left'></i> Previous", $links);

			$links = preg_replace("/Next &raquo;/", "Next <i class='fa fa-angle-right'></i>", $links);

			$links = preg_replace("/<a/", "<li class='page-item'><a", $links);
			$links = preg_replace("/<\/a>/", "</a></li>", $links);

			$links = preg_replace("/<span/", "<li class='page-item'><span", $links);
			$links = preg_replace("/<\/span>/", "</span></li>", $links);

			$links = preg_replace("/page-numbers/", "page-link", $links);

			$links = preg_replace("/<li class='page-item'><span class='page-link current/", "<li class='page-item active'><span class='page-link", $links);

			$template = '<ul class="pagination pagination-lg justify-content-center">%1$s</ul>';
			$navigation = sprintf($template, $links);
		}

		echo $navigation;

	}

	wp_reset_postdata( $args );
?>
<?php
/**
 * Template Name: Archive Page */
?>

<?php
$paged = (int) get_query_var('paged');
$args = array(
	'posts_per_page' => 3,
	'paged' => $paged,
	'orderby' => 'post_date',
	'order' => 'DESC',
	'post_type' => 'post',
	'post_status' => 'publish'
);
$the_query = new WP_Query($args);

if ( $the_query->have_posts() ) :
	while ( $the_query->have_posts() ) : $the_query->the_post();
		get_template_part( 'content', get_post_format() );
	endwhile;
else:
	get_template_part( 'content', 'none' );
endif;

if ($the_query->max_num_pages > 1) {
	echo paginate_links(array(
		'base' => get_pagenum_link(1) . '%_%',
		'format' => 'page/%#%/',
		'current' => max(1, $paged),
		'total' => $the_query->max_num_pages
	));
}

wp_reset_postdata();
?>

以上是关于php 将分页添加到页面模板的主要内容,如果未能解决你的问题,请参考以下文章

使用管道将分页添加到 dataflowjob 中的 Bigquery 查询

将分页添加到第 3 方模块 (OT-MiniNewsModule)

计算分页的偏移量(php)

将分页添加到 asp 转发器功能 ASP.Net

HTML DOM 使用循环将分页的 XML 文件合并为单个文件

wp查询分页和类别