【问题标题】:How to get post and add post nav link for Wordpress?如何获取帖子并为 Wordpress 添加帖子导航链接?
【发布时间】:2020-09-01 15:39:44
【问题描述】:

最近,我在 Wordpress 中使用 PHP 制作了一个自定义页面。我试图让所有帖子按特定标签过滤。这是我获取帖子并将其插入 div 的 PHP 代码:

<div class="container">
        <div class="row box">
            <?php
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
                'tag' => 'artikelseriesomb2020',
                'posts_per_page' => 10,
                'paged' => $paged,
            );
            $arr_posts = new WP_Query($args);

            if ($arr_posts->have_posts()) :

                while ($arr_posts->have_posts()) :
                    $arr_posts->the_post();
                    $url = wp_get_attachment_url(get_post_thumbnail_id($arr_post->ID), 'large');
            ?>
                    <div class="col-mt-4" style="padding:10px;">
                        <div class="card" style="width: 18rem;">
                            <img class="card-img-top" data-no-lazy="1" src="<?php echo $url; ?>" alt="Card image cap">
                            <div class="card-body">
                                <h5 class="card-title"><?php the_title(); ?></h5>
                                <p class="card-text"><?php the_excerpt(); ?></p>
                                <a href="<?php the_permalink(); ?>" class="btn btn-primary">BACA SELENGKAPNYA</a>
                            </div>
                        </div>
                    </div>
            <?php
                endwhile;
            endif;
            ?>
        </div>
    </div>

我想每页显示 10 个帖子,并在模板底部显示一个导航链接,以便用户可以单击另一个页面查看另一个/其余帖子。我试过这个单一的代码:

<?php posts_nav_link(); ?>

但它不显示任何导航链接。我的代码有问题吗?请帮帮我。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    请尝试此代码,它可能会对您有所帮助! 把它放在 IF 和 endwhile 之后。

    echo '<div class="post_pagination">';
    $total_pages = $arr_posts->max_num_pages;
    
    if ($total_pages > 1){
    
        $current_page = max(1, get_query_var('paged'));
    
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    echo '</div>';
    wp_reset_postdata()
    

    【讨论】:

    • 欢迎来到 Stack Overflow。感谢您的贡献,但请注意,不鼓励仅使用代码的答案,因为它们没有解释它如何解决问题。如果您编辑答案以解释此代码的作用以及它如何回答问题,它将对 OP 以及将来遇到类似问题的其他用户更有用。
    【解决方案2】:

    我找到了解决办法。

    这很简单!

    首先,我使用这个函数来生成分页:

    <?php
    function pagination_bar()
    {
        global $my_query;
    
        $total_pages = $my_query->max_num_pages;
    
        if ($total_pages > 1) {
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,
            ));
        }
    }
    ?>
    

    然后我调用函数:

    <?php
            if (have_posts()) :
                pagination_bar();
            endif;
            ?>
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2010-12-04
      • 1970-01-01
      相关资源
      最近更新 更多