Seach.php中的分页 - WP

时间:2017-11-20 05:58:10

标签: php wordpress templates search pagination

我无法在wordpress中的page.php中进行分页。这是代码:

global $query_string;
$ourCurrentPage = get_query_var('paged');
$post = array(
  'post_per_page' => 10,
  'paged' => $ourCurrentPage
);

$search_query = wp_parse_str( $query_string ,$post);
$search = new WP_Query( $search_query );
if ($search -> have_posts() ) {
  while ($search ->  have_posts() ) {
    $search ->  the_post();
    ...
    ...
  //rest of loop

在codex splain怎么做,但不能让它起作用

1 个答案:

答案 0 :(得分:0)

试试这段代码,

您可以在post_type属性中定义自定义类型名称,然后您可以获取带分页的列表帖子。



<?php
		$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $args = array(
            'post_type' => 'page',
            'orderby' => 'title',
            'order' => 'ASC',
            'posts_per_page' => 2,
            'paged' => $paged
        );

        $the_query = new WP_Query($args);

    ?>

    <?php if( $the_query->have_posts() ) : ?>

    <!-- options -->
    <div class="col-md-12 options border-bottom">

        <!-- pagination -->
        <ul class="pagination pull-right">
            <li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?></li>
            <li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
        </ul>

    </div>
    <!-- /.options -->

    <!-- cemeteries -->
    <div class="cemeteries">

        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>

            <!-- pages -->
            <div class="col-md-12">

                <div class="col-md-4">
                    <?php
                        if ( has_post_thumbnail() ) {
                            the_post_thumbnail('', array('class' => 'img-responsive'));
                        }
                    ?>
                </div>

                <div class="col-md-8">
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                    <p><a href="<?php the_permalink(); ?>"><button>Visit pages</button></a></p>
                </div>

            </div>
            <!-- /.pages -->

        <?php endwhile; ?>

    </div>
    <!-- /.cemetries -->

    <!-- options -->
    <div class="col-md-12 options border-bottom">

        <!-- pagination -->
        <ul class="pagination pull-right">
            <li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?></li>
            <li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
        </ul>

    </div>
    <!-- /.options -->

    <?php wp_reset_postdata(); ?>

    <?php else:  ?>

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

    <?php endif; ?>
&#13;
&#13;
&#13;

我希望此代码对您有所帮助