Pagination on Custom Post

时间:2015-10-06 作者:clestcruz

我想知道如何在wordpress模板上显示分页。这是我正在进行的工作。到目前为止,页面链接未显示

<div class="eight columns">
    <ul>
    <?php

        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $packages = new WP_Query( array( 
            \'post_type\' => \'articles\', 
            \'posts_per_page\' => 2, 
            \'orderby\'=> \'menu_order\', 
            \'paged\' => $paged
        ));

        if( $packages->have_posts() ) : while( $packages->have_posts() ) :
           $packages->the_post();
                ?>

        <li class="twelve columns">
            <div class="articles">
                <div class="contents">
                    <h2><?php the_title(); ?></h2>
                    <span><?php the_time(get_option(\'date_format\')); ?></span>
                        <div class="featured-image">
                            <?php if ( has_post_thumbnail()) { $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), \'large\'); echo \'<img src="\' . $large_image_url[0] . \'" title="\' . the_title_attribute(\'echo=0\') . \'" alt="\' . the_title_attribute(\'echo=0\') . \'">\';  } ?> 
                        </div>

                        <p><?php the_excerpt(); ?></p>
                </div>
            </div>

        </li>

        <?php  wp_reset_postdata(); ?>


        <?php endwhile; else: ?>

          <p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>

        <?php endif; ?>

        <div class="alignleft"><?php previous_posts_link(\'&laquo; Previous\') ?></div>
        <div class="alignright"><?php next_posts_link(\'More &raquo;\') ?></div>

    </ul>
</div>

1 个回复
SO网友:Robert hue

当您使用WP_Query 要从数据库获取帖子,必须使用next_posts_link() 具有max_num_pages 当前查询的参数。

您可以按以下方式执行此操作。

<?php next_posts_link(\'More &raquo;\', $packages->max_num_pages) ?>
现在您的代码将变成这样。我在下面的代码中做了上述更改。

<div class="eight columns">
    <ul>
    <?php

        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $packages = new WP_Query( array( 
            \'post_type\' => \'articles\', 
            \'posts_per_page\' => 2, 
            \'orderby\'=> \'menu_order\', 
            \'paged\' => $paged
        ));

        if( $packages->have_posts() ) : while( $packages->have_posts() ) :
           $packages->the_post();
                ?>

        <li class="twelve columns">
            <div class="articles">
                <div class="contents">
                    <h2><?php the_title(); ?></h2>
                    <span><?php the_time(get_option(\'date_format\')); ?></span>
                        <div class="featured-image">
                            <?php if ( has_post_thumbnail()) { $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), \'large\'); echo \'<img src="\' . $large_image_url[0] . \'" title="\' . the_title_attribute(\'echo=0\') . \'" alt="\' . the_title_attribute(\'echo=0\') . \'">\';  } ?> 
                        </div>

                        <p><?php the_excerpt(); ?></p>
                </div>
            </div>

        </li>

        <?php  wp_reset_postdata(); ?>


        <?php endwhile; else: ?>

          <p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>

        <?php endif; ?>

        <div class="alignleft"><?php previous_posts_link(\'&laquo; Previous\') ?></div>
        <div class="alignright"><?php next_posts_link(\'More &raquo;\', $packages->max_num_pages) ?></div>

    </ul>
</div>

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x