[英]get_posts on a WP static page
这是我第一次在这个论坛上提问。 我希望我能足够具体。 我正在尝试替换WP模板的首页帖子部分。 当WP主题设置为settings> reading>您的最新帖子时,我的index.php页面上的以下代码运行良好(但获得了帖子),但将WP主题“ thoughter”的设置方式设置为static页面和帖子是通过博客模板页面(与index.php文件位于同一位置)获取的,该页面从单独包含的文件中的循环中获取帖子。 我出于某些原因希望将其设置为静态。 我的问题是:“下面的代码仅在index.php文件中有效,而在与index.php文件位于同一位置的Blog模板文件中有效,是有原因的吗?我已经检查了代码中调用的模板部分被调用,似乎没有帖子要发布(有)。
感谢您的时间,
戴夫
<!-- blog content -->
<div class="container">
<div class="row" id="primary">
<main id="content" class="col-sm-8" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'template-parts/content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- content -->
<!-- sidebar -->
<aside class="col-sm-4">
<?php get_sidebar(); ?>
</aside>
</div><!-- primary -->
</div><!-- container -->
have_posts()函数仅允许您检查页面上是否有该帖子,并且由于它是您的静态页面,因此不会有任何帖子分配给该页面。
您需要先在页面开头查询以显示帖子。 这是例子。
编辑::
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'term_id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
您可以在下面的链接中找到有关WP_Query的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.