【发布时间】:2020-01-21 08:56:43
【问题描述】:
我试图在招聘网站的主页上展示 2 个帖子,当没有帖子时,我想在主页上显示一条消息,说明“目前没有机会开放”。
我已经设法做到了其中之一,但无法让两者同时发挥作用。
更新:如果没有帖子,我现在可以显示消息,但是主页不再包含两个帖子,它只有一个....
<?php $the_query = new WP_Query( 'posts_per_page=2' ); ?>
<?php if ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="block-halves exec-ops" ><!--id="b-10"-->
<span class="block-content">
<h3><a href="<?php the_permalink() ?>"><?php the_field('post_title');?></a></h3>
<span class="btm">
<p class="job-title"><?php the_field('job_title');?></p>
<p><?php echo excerpt(22); ?></p>
<p><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_field('cta_button_home');?></a></p>
</span>
</span>
</div>
<?php else: ?>
<div class="block-halves exec-ops" ><!--display if no posts available-->
<span class="block-content">
<h3>There are currently no opportunities open</h3>
<span class="btm">
<p class="job-title">Submit Your CV</p>
<p>We actively search for suitable roles and select the top candidates from submitted CV's when new opportunities open up.</p>
<p><a href="<?php echo get_page_link(47); ?>">Submit</a></p>
</span>
</span>
</div>
<?php endif; wp_reset_postdata();?>
</div><!--flex-row/end-->
【问题讨论】:
-
您可以尝试 id 并检查 $the_query 是否为空?像 那么你的代码并关闭括号 for if 循环。
-
感谢您的回复 - 不幸的是这不起作用
-
WP_Query 对象的
post_count属性保存返回的帖子数,所以使用它。 -
查看上面的更新:)
标签: php wordpress if-statement posts