如何在自定义wordpress页面上添加页面分页?
我有一个自定义模板,显示特定类别的帖子。当我在页面底部添加_posts_pagination()函数时,它不会生成任何分页链接。我的代码有什么问题 我是WordPress新手,对php知之甚少,因此我不知道如何追踪代码不起作用的原因如何在自定义wordpress页面上添加页面分页?,wordpress,pagination,custom-wordpress-pages,Wordpress,Pagination,Custom Wordpress Pages,我有一个自定义模板,显示特定类别的帖子。当我在页面底部添加_posts_pagination()函数时,它不会生成任何分页链接。我的代码有什么问题 我是WordPress新手,对php知之甚少,因此我不知道如何追踪代码不起作用的原因 <?php global $wp_query; $original_page_id = get_the_ID(); ?> <article <?php post_class(); ?&
<?php
global $wp_query;
$original_page_id = get_the_ID();
?>
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<section class="page-content">
<?php
$args = array ('category_name' => 'insights', 'tag' => 'for_business', 'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
);
$catPost = get_posts($args);
foreach ($catPost as $post) : setup_postdata($post->post_excerpt); ?>
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
<p style="font-size: 12px"><?php the_time('j F Y'); ?> | <?php the_author_posts_link(); ?></p>
<div style="color: black"><?php the_excerpt(); ?></div>
<?php endforeach;
the_posts_pagination(
array(
'prev_text' => __( 'Previous page', 'a2b' ),
'next_text' => __( 'Next page', 'a2b' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'a2b' ) . ' </span>',
)
);
?>
</section>
</article>
' ); ?>
|
The_posts_pagination()
仅适用于主循环(全局$wp_查询)WordPress。您正在使用自己的请求(get\u posts
)。因此,您可以使用paginate_links()
来进行此操作
global $paged;
$paged = max( $paged, 1 );
$per_page = 10;
$query. = new WP_Query(
[
'category_name' => 'insights',
'tag' => 'for_business',
'paged' => $paged,
'posts_per_page' => $per_page,
]
);
if ( $query->have_posts() ) {
foreach ( $query->posts as $post ) {
// ...
}
}
$total = ceil( $query->found_posts / $paged );
echo paginate_links(
[
'total' => 1,
'current' => $paged,
]
);
这还不够吗
我有类似的东西(自定义帖子列表,也可以根据类别进行筛选):
$tax\u slug=get\u query\u var('portfolio\u type');
//参数,可以有一个tax_查询,也可以只有“常规”查询参数。
$args=[
“post_type”=>“portfolio”,
“每页帖子数”=>3,
'orderby'=>'date',
“订单”=>“描述”
];
//当我显示分页时
如果($show_pagination){
$paged=(获取查询变量('paged'))?获取查询变量('paged'):1;
$args['paged']=$paged;
}
//如果单击了分类法,我将存储它并将其添加到$args数组中
如果(!空($tax\u slug)){
$args['tax_query']=[[
“分类法”=>“投资组合类型”,
'字段'=>'段塞',
“术语”=>tax\u slug美元,
]];
}
//运行WP_查询()
$query=新的WP\u查询($args);
回显分页链接(数组(
'base'=>str_replace(99999999,%#%',esc_url(get_pagenum_link(99999999)),
'total'=>$query->max\u num\u pages,//WP\u query返回最大
“当前”=>max(1,获取查询变量('paged')),
'格式'=>'?分页=%#%',
“全部显示”=>正确,
'类型'=>'普通',
“端部尺寸”=>2,
“中等大小”=>1,
“上一步”=>正确,
'prev_text'=>sprintf('%1$s','Vorige Projecten','km default'),
'next_text'=>sprintf('%1$s','Volgende Projecten','km default'),
'add_args'=>false,
'添加片段'=>'',
) );
嗨,WP朋克!谢谢你的回复。我已经更改了代码(请参见UPD),但它没有显示分页。此外,它不会显示现在自动生成的摘录。我对代码做了什么错误?是的,当然不要忘记WP_查询中的“每页帖子”
global $paged;
$paged = max( $paged, 1 );
$per_page = 10;
$query. = new WP_Query(
[
'category_name' => 'insights',
'tag' => 'for_business',
'paged' => $paged,
'posts_per_page' => $per_page,
]
);
if ( $query->have_posts() ) {
foreach ( $query->posts as $post ) {
// ...
}
}
$total = ceil( $query->found_posts / $paged );
echo paginate_links(
[
'total' => 1,
'current' => $paged,
]
);
$tax_slug = get_query_var( 'portfolio_type' );
// Arguments which can either have a tax_query, or just the "regular" query arguments.
$args = [
'post_type' => 'portfolio',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC'
];
// When I'm showing the pagination
if ( $show_pagination) {
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args['paged'] = $paged;
}
// If a taxonomy was clicked, I store and add it to the $args array
if ( ! empty( $tax_slug ) ) {
$args['tax_query'] = [[
'taxonomy' => 'portfolio_type',
'field' => 'slug',
'terms' => $tax_slug,
]];
}
// Run WP_Query()
$query = new WP_Query( $args );
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages, // WP_Query returns the max
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => true,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<span class="arrow" aria-hidden="true"><i class="fas fa-angle-left"></i></span> %1$s', __( 'Vorige Projecten', 'km-default' ) ),
'next_text' => sprintf( '%1$s <span class="arrow" aria-hidden="true"><i class="fas fa-angle-right"></i></span>', __( 'Volgende Projecten', 'km-default' ) ),
'add_args' => false,
'add_fragment' => '',
) );