我的索引文件中有一个自定义的Wordpress循环,该循环当前不起作用。此自定义WP循环的目的是根据其帖子编号分配不同的类和结构。
不幸的是,下面的代码可在index.php文件中正常运行,但是将其复制到自定义页面模板后无法正常工作。
<?php
/**
* Template Name: custom page template
*/
get_header(); ?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="item1">
<span>hello!</span<?php the_title(); ?>>
</div><!-- .item# -->
<?php elseif ($count == 2) : ?>
<div class="item2">
<?php the_title(); ?><span>Hi!</span
</div><!-- .item# -->
<?php elseif ($count == 3) : ?>
<div class="item3">
<!-- Put Your Stuff Here -->
</div><!-- .item# -->
<?php elseif ($count == 4) : ?>
<div class="item4">
<!-- Put Your Stuff Here -->
</div><!-- .item# -->
<?php elseif ($count == 5) : ?>
<div class="item5">
<!-- Put Your Stuff Here -->
</div><!-- .item# -->
<?php else : ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
目标:
我要实现的目标是创建一个自定义页面(比方说)www.mywebsite.com/my-custom-page,其中列出了所有文章。
如上所述,自定义循环不显示在页面中,也不显示编号的分页。好像页面模板无法识别或忽略自定义循环代码。
我已经尝试过使用WP查询,但还是没有运气。下面的代码返回“对不起,没有符合您条件的帖子。”
部分工作的WP查询代码
这是我的网站,此代码将出现,但似乎不起作用
<?php
/**
* Template Name: Custom Page - Blog
*/
get_header(); ?>
<!-- START of WP Query -->
<?php $the_query = new WP_Query( array("post_type"=>'post')); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="item1">
<span>Post 1</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 2) : ?>
<div class="item2">
<span>Post 2</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 3) : ?>
<div class="item3">
<span>Post 3</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 4) : ?>
<div class="item4">
<span>Post 4</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 5) : ?>
<div class="item5">
<span>Post 5</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 5 || $count <= 7) : ?>
<div class="item6">
<span>Post 6 to 7</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 8 || $count <= 15) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 16) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
Run Code Online (Sandbox Code Playgroud)
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
<?php else : ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<!-- END of WP Query -->
<?php get_footer(); ?>
</article>
<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助。谢谢!
As pointed in the previous answers, you can use WP_Query
to make a custom query for posts, custom post types (CPT) and pages:
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
// other args here
) );
Run Code Online (Sandbox Code Playgroud)
And use The Loop to display the posts:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//
// Post Content here
//
} // end while
} // end if
Run Code Online (Sandbox Code Playgroud)
Now referring to this:
Partially Working WP Query Code
Here's my website where this code will appear but seems the not working
I think you meant to say, "the pagination is not working", right? Because it's not:
Because you're using the global $wp_query
object with your pagination.
In your WP_Query
construct, you didn't set the paged
parameter which is needed for the pagination to work properly.
So here's how it should be:
$current_page = max( 1, get_query_var( 'paged' ) ); // the current page
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $current_page,
// other args here
) );
Run Code Online (Sandbox Code Playgroud)
And then use the $current_page
with paginate_links()
— you can also see here, I used $the_query
and not $wp_query
when retrieving/specifying the max number of pages:
echo paginate_links( array(
'current' => $current_page,
'total' => $the_query->max_num_pages, // here I don't use $wp_query
// other args here
) );
Run Code Online (Sandbox Code Playgroud)
And below is a working code you can use in place of your partially working code (the one in between the <!-- START of WP Query -->
and <!-- END of WP Query -->
):
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $current_page,
// other args here
) );
if ( $the_query->have_posts() ) :
$count = 1;
while ( $the_query->have_posts() ) : $the_query->the_post();
if ( 1 === $count ) :
?>
<div class="item item1" style="background: red; color: #fff;">
<span>Post 1</span> <?php the_title( '<h3>', '</h3>' ); ?>
</div>
<?php
elseif ( 2 === $count ) :
?>
<div class="item item2" style="background: orange; color: #fff;">
<span>Post 2</span> <?php the_title( '<h3>', '</h3>' ); ?>
</div>
<?php
// other conditions here
else :
?>
<div class="item item3" style="background: yellow; color: #666;">
<span>Post <?php echo $count; ?></span>
<?php the_title( '<h3>', '</h3>' ); ?>
</div>
<?php
endif;
$count++;
endwhile;
?>
<p>Pagination:</p>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $the_query->max_num_pages,
'type' => 'list',
'end_size' => 3,
) );
?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_postdata();
?>
Run Code Online (Sandbox Code Playgroud)