主页上的分页WordPress

我在尝试分页以便在我正在工作的网站的主页上工作时遇到问题。

这是我正在使用的代码, 当然已简化:

如何对此进行分页?

<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
    <?php query_posts($args); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();    ?>

            <article class="col-md-12">
                <a href="<?php the_permalink(); ?>" >
                    <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                    <div class="entry entry-table">
                        <div class="title">
                          <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                          <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                       <p><?php echo get_excerpt(228, 'content'); ?>
                        <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                       </p>
                        </div>
                    </div>

            </article>

        <?php endwhile; endif; ?>

#1


<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
    <?php query_posts($args); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();    ?>

            <article class="col-md-12">
                <a href="<?php the_permalink(); ?>" >
                    <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                    <div class="entry entry-table">
                        <div class="title">
                          <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                          <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                       <p><?php echo get_excerpt(228, 'content'); ?>
                        <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                       </p>
                        </div>
                    </div>

            </article>

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

#2


首先, 你必须正确设置查询:

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

其次, 你需要在循环外添加分页链接

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

整个代码如下所示:

<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
?>
    <?php query_posts($args); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();    ?>

            <article class="col-md-12">
                <a href="<?php the_permalink(); ?>" >
                    <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                    <div class="entry entry-table">
                        <div class="title">
                          <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                          <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                       <p><?php echo get_excerpt(228, 'content'); ?>
                        <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                       </p>
                        </div>
                    </div>

            </article>

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

#3


问题已解决:

<?php
global $paged, $wp_query, $wp;
$args1 = array('cat' => '3, 7, 10, 12');

$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=3&cat=3, 7, 10, 12');
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article class="col-md-12">
                <a href="<?php the_permalink(); ?>" >
                    <div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
                    <div class="entry entry-table">
                        <div class="title">
                          <a href="<?php the_permalink(); ?>" ><h3 class="h5"><?php the_title(); ?></h3></a>
                          <span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
                       <p><?php echo get_excerpt(228, 'content'); ?>
                        <a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
                       </p>
                        </div>
                    </div>

            </article>
 <?php endwhile; ?>
 <?php wp_pagenavi(); ?>
 <?php $wp_query = null; $wp_query = $temp;?>

来源:

https://www.srcmini02.com/63750.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37582.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?