静态页面上每页的帖子+分页

时间:2015-09-18 04:08:03

标签: php wordpress pagination posts static-site

感谢您花时间检查一下。

我花了几天时间来讨论这件事。在wp.org上有很多时间,但我还没有得到它。应该是一个简单的解决方案,我确定。无论我尝试什么,我都不能限制每页的帖子数量,也不能显示任何分页。这是我最近的尝试(也许不是我最好的尝试)。该页面只显示所有帖子或所有最近的帖子(不是我的网站)。一旦我至少可以获得限制帖子的页面,那么我将解决这个问题。此外,在WP仪表板中设置每页的帖子什么都不做......从来没有做过。这就是为什么我自己尝试编写代码的原因。为什么我不能限制每页的帖子数量?我会把这个分页放在我目前拥有的地方吗?这是一团糟吗?哈哈?

再次感谢,

戴夫(下面的代码)

<?php /* Template Name: Stories */ ?>

<?php 

get_header(); ?>

    <!-- *************************************** -->    
    <div class="custom_two_third">

<?php

// The Query

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );



$the_query = new WP_Query($args);

// The Loop
if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        get_template_part( 'template-parts/content', get_post_format() );
    }

// pagination
next_posts_link();
previous_posts_link();

} else {
    get_template_part( 'template-parts/content', 'none' );
}



/* Restore original Post Data */
wp_reset_postdata();
?>

    <div class="clear"></div>

    </div><!-- custom_two_third -->
<?php 

    get_sidebar(); 

    get_footer(); 

?>

3 个答案:

答案 0 :(得分:0)

尝试使用post_type

$args_articles =array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 5,
            'order' => 'desc',
            'paged' => $paged
    );

答案 1 :(得分:0)

// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_eve_args = array( 'post_type' => 'news_events', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $news_eve_args );
if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
          <?php the_title(); echo "<br/>"; ?>
    <?php endwhile; ?>
    <nav>
      <?php previous_posts_link('&laquo; Newer',$wp_query->max_num_pages); ?>
      <?php next_posts_link('Older &raquo;',$wp_query->max_num_pages); ?>
    </nav>
    <?php wp_reset_postdata(); ?>                           
    <?php else : ?>
        <p><?php _e( 'Sorry, no news or events  at this time.', 'theme' ); ?></p>
<?php endif; ?>

答案 2 :(得分:-1)

尝试这个,它将适用于你的情况。

global $query_string;
query_posts("{$query_string}&posts_per_page=12");

while (have_posts()) {
    the_post();
    ...
}
相关问题