我需要在帖子中插入分页而不是加载更多按钮,但我不知道该怎么做。我需要下一页和上一页或数字的分页。
这是我们的开发工作,但我不知道从哪里开始,也不知道应该把 this 示例放在哪里。
<?php
/*
* Template Name: Blog
* description: >-
Page template without sidebar
*/
get_header(); ?>
<body class=" pageHeader--gray">
<div class="wrapper">
<div class="content">
<div class="pageHeader">
<div class="container animate">
<h1 class="pageHeader__title"><?php the_field('title'); ?>
</h1>
</div>
<div class="pageHeader__background">
<svg xmlns="http://www.w3.org/2000/svg" width="1920" viewBox="0 0 1920 126">
<path fill="#FFF" fill-rule="evenodd" d="M9.09494702e-13,7.95807864e-13 L1920,7.95807864e-13 L1920,22.1174168 C1622.41146,91.3724723 1302.41146,126 960,126 C617.588542,126 297.588542,91.3724723 9.09494702e-13,22.1174168 L9.09494702e-13,7.95807864e-13 Z" transform="rotate(180 960 63)"></path>
</svg>
</div>
<div class="pageHeader__decor animate animate--wrapper">
<div class="pageHeader__decor-1 animate__child">
</div>
<div class="pageHeader__decor-2 animate__child">
</div>
</div>
</div>
<div class="blog">
<div class="container">
<div class="blog__tags animate">
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$active = "";
if ( null == @$_GET['category_id'] ) {
$active = "active";
}
echo '<a class="blog__tag '.$active.'" href="'. site_url() .'/blog">All Categories</a>';
foreach( $categories as $category ) {
$active = "";
if ( $category->name == @$_GET['category_id']) {
$active = "active";
}
echo '<a class="blog__tag '.$active.'" href="'. site_url() .'/blog?category_id='.$category->name.'">'.$category->name.'</a>';
}
?>
</div>
<div class="blog__wrapper">
<?php
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1,
'category_name' => @$_GET['category_id']
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<a class="newsCard newsCard--big animate" href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail( get_the_ID() ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<div class="newsCard__image"><img src="<?php echo $image[0]; ?>" alt=""/></div>
<?php endif; ?>
<div class="newsCard__content">
<div class="newsCard__tag">
<?php
$categories="";
foreach((get_the_category()) as $category){
$categories = $category->name.",";
}
echo substr($categories, 0, -1);
?>
</div>
<div class="newsCard__title"><?php the_title(); ?>
</div>
<div class="newsCard__description"><?php the_excerpt(); ?>
</div>
<div class="newsCard__link">Learn more
<div class="newsCard__link-icon">
</div>
</div>
</div>
</a>
<?php
endwhile;
wp_reset_postdata();
?>
<?php
$load = 0;
$total_post =wp_count_posts()->publish;
if (null !== @$_GET['load']){
$load = $_GET['load'];
}
$i = 6 + $load;
?>
<?php
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page'=>$i,
'ignore_sticky_posts' => 1,
'category_name' => @$_GET['category_id']
));
$count = $wpb_all_query->found_posts;
?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<a class="newsCard newsCard animate" href="<?php the_permalink(); ?>">
<?php if (has_post_thumbnail( get_the_ID() ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<div class="newsCard__image"><img src="<?php echo $image[0]; ?>" alt=""/></div>
<?php endif; ?>
<div class="newsCard__content">
<div class="newsCard__tag">
<?php
$categories="";
foreach((get_the_category()) as $category){
$categories = $category->name.",";
}
echo substr($categories, 0, -1);
?>
</div>
<div class="newsCard__title"><?php the_title(); ?>
</div>
<div class="newsCard__description"><?php the_excerpt(); ?>
</div>
<div class="newsCard__link">Learn more
<div class="newsCard__link-icon">
</div>
</div>
</div>
</a>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php if( null == @$_GET['load'] && $count > 6 ) : ?>
<div class="blog__button animate">
<?php
if ( null == @$_GET['category_id'] ) {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
} else {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?category_id=<?php echo $_GET['category_id']; ?>&load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
}
?>
</div>
<?php endif; ?>
</div>
</div>
<div class="achievments">
<div class="container">
<div class="blockTitle blockTitle--center blockTitle--small">
<h2><?php the_field('achievement_title'); ?></h2>
</div>
<div class="rewardsCards2 animate animate--wrapper">
<?php
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'achievement_cpt',
'post_status'=>'publish',
'posts_per_page'=>-1,
// 'ignore_sticky_posts' => 1,
// 'meta_key' => 'departament',
// 'meta_value' => 'Information Technology and Security'
));
?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div class="rewardsCards2__item animate__child"><img src="<?php echo get_field('image', get_the_ID()); ?>" alt=""/>
<div class="rewardsCards2__tooltip">
<div class="rewardsCards2__tooltip-logo"><img src="<?php echo get_field('image', get_the_ID()); ?>" alt=""/>
</div>
<div class="rewardsCards2__tooltip-content">
<div class="rewardsCards2__tooltip-title"><?php echo get_field('title', get_the_ID());?>
</div>
<div class="rewardsCards2__tooltip-description"><?php echo get_field('content', get_the_ID());?>
</div><a class="rewardsCards2__tooltip-link" href="<?php echo get_field('url',get_the_ID());?>">Read More</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer('phone'); ?>
</div>
</body>
<?php get_footer(); ?>
答案 0 :(得分:0)
所以我不会重复已经说过的in the article,但我希望这些对您有所帮助:
删除 $load
(或 $_GET['load']
)和 $i
部分:
// Find and remove this:
$load = 0;
$total_post =wp_count_posts()->publish;
if (null !== @$_GET['load']){
$load = $_GET['load'];
}
$i = 2 + $load;
在第一个 paged
中添加 // the query
。我们还设置了一个静态 posts_per_page
,不过您可以稍后将其设为动态。
// Find and replace this:
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page'=>$i,
'ignore_sticky_posts' => 1,
'category_name' => @$_GET['category_id'],
));
$count = $wpb_all_query->found_posts;
// With this one:
// the query
$wpb_all_query = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
// Set a static posts_per_page value.
'posts_per_page'=> 6,
'ignore_sticky_posts' => 1,
'category_name' => @$_GET['category_id'],
// Add the "paged" arg.
'paged' => max( get_query_var( 'paged' ), 1 ),
));
// The $count is not needed (anymore).
之后,找到并替换这个:
<?php if( null == @$_GET['load'] && $count > 6 ) : ?>
<div class="blog__button animate">
<?php
if ( null == @$_GET['category_id'] ) {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
} else {
?>
<a class="btn" href="<?php echo site_url(); ?>/blog?category_id=<?php echo $_GET['category_id']; ?>&load=<?php echo $load + $total_post; ?>"><?php the_field('load_more_button_label'); ?></a>
<?php
}
?>
</div>
<?php endif; ?>
使用以下任意一种,无论您喜欢哪种(简单或数字分页):
这里,我们使用 previous_posts_link()
和 next_posts_link()
。
<?php
if ( $wpb_all_query->max_num_pages > 1 ) :
$paged = max( get_query_var( 'paged' ), 1 );
?>
<div class="simple-pagination"><?php
previous_posts_link( '← Older Posts' );
if ( $paged > 1 && $paged < $wpb_all_query->max_num_pages ) {
echo ' <span class="sep">•</span> ';
}
next_posts_link( 'Newer Posts →', $wpb_all_query->max_num_pages );
?></div>
<?php endif; ?>
这里,我们使用的是 paginate_links()
— 有关参数的更多详细信息,请查看函数参考。
<?php
$links = paginate_links( array(
'total' => $wpb_all_query->max_num_pages,
'prev_text' => '← Older Posts',
'next_text' => 'Newer Posts →',
) );
if ( $links ) {
echo "<div class='numeric-pagination'>$links</div>";
}
?>
在您的 new WP_Query()
调用中,参数需要具有 paged
参数,以便 WordPress 知道当前页码,然后仅检索该特定页面的帖子。
使用 next_posts_link()
和 paginate_links()
,您需要传递 $wpb_all_query->max_num_pages
,这是您自定义 WordPress 查询的总页数({ {1}})。
仅此而已,再次,我希望这个答案有所帮助,并注意我在上面的示例中只使用了基本的 HTML,因此只需根据您的喜好对其进行修改即可。
答案 1 :(得分:0)
将此代码粘贴到functions.php中
<?php
//Custom pagination
function the_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 '<ul class="pagination " itemscope itemtype="http://schema.org/SiteNavigationElement/Pagination">' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( ' <li class="page-item">%s</li>
' . "\n", get_previous_posts_link('
<span aria-hidden="true" class="page-link ripple">«</span>
<span class="sr-only">Previous</span>
') );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="page-item active"' : '';
printf( '<li%s class="page-item"><a href="%s" class="page-link ripple" itemprop="relatedLink/pagination">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="page-item active"' : '';
printf( '<li%s class="page-item"><a href="%s" class="page-link ripple" itemprop="relatedLink/pagination">%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>…</li>' . "\n";
$class = $paged == $max ? ' class="page-item active"' : '';
printf( '<li%s class="page-item"><a href="%s" class="page-link ripple" itemprop="relatedLink/pagination">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li class="page-item ">%s</li>
' . "\n", get_next_posts_link('<span aria-hidden="true" class="page-link ripple">»</span>
<span class="sr-only">Next</span>') );
echo '</ul>' . "\n";
}
?>
并在需要分页的地方使用 <?php the_pagination(); ?>