php - 如何在 WordPress 中使 HTML 元素不在循环内循环
问题描述
这有可能在WordPress中制作这样的帖子列表吗?
<div class="large-image">
<img src="http://example.com">
<h1>Title</h1>
</div>
<div class="post-row"><!-- this is not a loop -->
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
</div>
我尝试使用下面的代码,但<div class="post-row">
始终包含在循环中。
<?php
if ( have_posts() ) : $post_count = 0;
while ( have_posts() ) : the_post();
if ( $post_count = 0 ) { ?>
<div class="large-image">
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php } ?>
<div class"post-row">
<?php if ( $post_count > 0 ) { ?>
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php } ?>
</div><!-- /post-row -->
<?php
endwhile
endif;
?>
我想要的<div class="post-row">
不是循环。
我在stackoverflow中尝试了一些答案,但post-row
总是循环。
希望我得到最好的答案。
解决方案
您必须将后行 div 移动到 while 循环上方,您只是错过了放置它而已
<?php if ( have_posts() ) : $post_count = 0;
if ( $post_count == 0 ) { ?>
<div class="large-image">
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php } ?>
<div class"post-row">
<?php
while ( have_posts() ) : the_post();
if ( $post_count > 0 ) { ?>
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php }
$post_count ++;
endwhile; ?>
</div><!-- /post-row -->
<?php endif; ?>
推荐阅读
- javascript - echart 数据点渐变颜色
- microsoft-graph-api - 我是否可以为 Microsoft Teams 频道动态配置传入 Webhook?
- python - 如何在 Python 中的不同进程之间发送消息?
- typeahead.js - Typeahead/Bloodhound 只返回第一个搜索结果而不是新搜索?
- coq - Coq 中的“intro ->”在上下文中是如何工作的?
- excel - Web 数据提取未更新
- mysql - MySQL 允许在定义为 NO NULLS 的字段中使用 NULL
- xamarin.forms - Xamarin Forms Entry 更改阿拉伯语的流向问题
- r - “缺少参数‘模式’” - 在调用 readRDS() 后出现在每个命令之后
- selenium - Selenium side runner 在多个浏览器上并行运行测试