簡體   English   中英

wordpress帖子中的分頁

[英]Pagination in wordpress posts

我已經獲得了這個帖子模板,我正在嘗試將帖子分頁,我將如何實現這一目標? 如果我以一種天真的方式詢問這個/接近這個,我會道歉。

我試過跟隨WordPress指南,但它們似乎與我所得到的並不匹配,我正在努力推斷。

希望這一切都有意義,非常感謝。

<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
<?php $postLoops = 0 ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php $postLoops++;
if ($postLoops == 2) {
    echo '<div class="post-right-half">';
}
if ($postLoops == 1) {
    echo '<div class="post-left-half">';
}

?>
<div class="post-square">
    <div class="post-thumbnail" style="background-image: url(<?php the_post_thumbnail_url(); ?>);"><br></div>
    <div class="content-half">
        <h2 class="post-title"><?php the_title(); ?></h2>
        <p class="post-content"><?php the_excerpt(__('(more…)')); ?</p>
        <a class="read-more" href="<?php the_permalink() ?>">Read More <i class="fa fa-angle-right" aria-hidden="true"></i></a>
    </div>
</div>

<?php
if ($postLoops == 3) {
    echo '</div>';
}
if ($postLoops == 1) {
    echo '</div>';
}
?>

<?php
endwhile;
wp_reset_postdata();
?>

您可以使用the_posts_pagination()顯示分頁鏈接。

它是一個模板標簽,因此它顯示分頁(不需要echo顯結果)。

https://codex.wordpress.org/Function_Reference/the_posts_pagination

如果您希望首先獲得標記並在回顯之前處理它,那么get_the_posts_pagination()就是您的朋友。

https://codex.wordpress.org/Function_Reference/get_the_posts_pagination

所以我不知道這是多么普遍,但是從評論文檔鏈接的組合和一些進一步的研究中,這是我得到的解決方案:

前兩行變為:

<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
<?php $the_query = new WP_Query( array('posts_per_page' => 12,'paged' => $paged) ); ?>
<?php $postLoops = 0 ?>

這有點復雜,但我認為它的核心是我必須將查詢設置轉換為一個數組,其中包含上面定義的分頁變量(不確定我是否完全理解了這一點,但我正在研究它) 。

然后在我的頁面底部,我希望我的鏈接出現,我放入了我們修改的瘋狂數組,以適應我們需要它做的事情,這看起來很復雜,但基本上只包括您可能需要的所有設置,包括設置上一個和下一個按鈕作為字體真棒按鈕:

<?php 
    echo paginate_links( array(
        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
        'total'        => $the_query->max_num_pages,
        'current'      => max( 1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => true,
        'prev_text'    => sprintf( '<i></i> %1$s', __( '<i class="fa fa-angle-left" aria-hidden="true"></i>', 'text-domain' ) ),
        'next_text'    => sprintf( '%1$s <i></i>', __( '<i class="fa fa-angle-right" aria-hidden="true"></i>', 'text-domain' ) ),
        'add_args'     => false,
        'add_fragment' => '',
    ) );
?>

我認為這就是一切,如果我更了解它,我會添加更多描述,但它現在似乎有效。

非常感謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM