我正在尝试在主题内实现分页。我想使用Understrap主题分页。因此,我下载了this file,并在functions.php中添加了require代码。接下来,我在循环中添加了代码,但是...不起作用。我的代码有什么问题?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => $paged,
'cat' => '-6'
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$background = get_the_post_thumbnail_url(get_the_ID(), 'medium');
$category = get_the_category();
$category_class = $category[0]->slug;
?>
<div class="row news__item">
<div class="col-md col-lg-4">
<a href="<?php the_permalink(); ?>">
<div class="post-listing__image <?php if ( has_post_thumbnail() ) {} else {echo "post-listing__image-default";}; ?>" style="background: url('<?php echo $background; ?>');"></div>
</a>
</div>
<div class="col-md col-lg-8">
<div class="info-bar">
<span class="badge badge-pill mm-badge">dodano: <?php echo get_the_date(); ?></span>
<span class="badge badge-pill mm-badge <?php echo $category_class; ?>"><?php the_category(' '); ?></span>
</div>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" class="btn btn-default btn-mm btn-mm--green">Więcej</a>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
else:
?>
<h5>Brak wyników spełniających kryteria wyszukiwania</h5>
<?php
endif;
?>
<div class="row">
<div class="col-md">
<!-- pagination here -->
<?php mmtemplate_pagination(); ?>
</div>
</div>
答案 0 :(得分:0)
您的WP查询似乎存在一些问题。尝试使用此代码,让我知道。
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => $paged,
'cat' => '-6'
);
$wp_query=new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$background = get_the_post_thumbnail_url(get_the_ID(), 'medium');
$category = get_the_category();
$category_class = $category[0]->slug;
?>
<div class="row news__item">
<div class="col-md col-lg-4">
<a href="<?php the_permalink(); ?>">
<div class="post-listing__image <?php if ( has_post_thumbnail() ) {} else {echo "post-listing__image-default";}; ?>" style="background: url('<?php echo $background; ?>');"></div>
</a>
</div>
<div class="col-md col-lg-8">
<div class="info-bar">
<span class="badge badge-pill mm-badge">dodano: <?php echo get_the_date(); ?></span>
<span class="badge badge-pill mm-badge <?php echo $category_class; ?>"><?php the_category(' '); ?></span>
</div>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" class="btn btn-default btn-mm btn-mm--green">Więcej</a>
</div>
</div>
<?php endwhile; ?>
<div class="row">
<div class="col-md">
<!-- pagination here -->
<?php mmtemplate_pagination(); ?>
</div>
</div>
<?php
wp_reset_postdata();
else:
?>
<h5>Brak wyników spełniających kryteria wyszukiwania</h5>
<?php
endif;
?>
更新: 您不应在“ template_part”文件中调用循环。它应该在主文件中,请参见下面的代码。
<section class="section post-listing">
<div class="container">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => $paged,
'cat' => '-6'
);
$wp_query=new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
get_template_part('template-parts/loop/loop', 'post-listing');
endwhile;
?>
<div class="row">
<div class="col-md">
<!-- pagination here -->
<?php mmtemplate_pagination(); ?>
</div>
</div>
<?php
wp_reset_postdata();
else:
?>
<h5>Brak wyników spełniających kryteria wyszukiwania</h5>
<?php
endif;
?>
</div>
</section>