html - 在 Wordpress 上显示来自特定类别的随机帖子
问题描述
我正在使用以下代码在每次刷新时显示来自“主页”类别中的随机帖子的两个图像(我已使用高级自定义字段设置)。但是它似乎不起作用,当我用谷歌搜索它时,有很多不同的方法可以做到!有人知道我的看起来是否正确吗?我担心我可能会丢失一个结束标签或一些小东西。
<div class="feature-container">
<?php
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category_name' => 'homepage',
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
if( have_rows('images') ):
while ( have_rows("images") ) :
the_row();
if ( get_sub_field( 'image' ) ): ?>
<a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>">
<div class="image feature-item">
<div class="overlay" style="background-color:<?php the_sub_field('hover_colour');?>">
<div class="caption pm-big">
<?php
$link = get_sub_field('link');
if( $link ): ?>
<?php echo $link['title']; ?>
<?php endif; ?>
</div>
</div>
<img src="<?php the_sub_field('image');?>" alt="" style="display: block;">
</div>
</a>
<?php else: ?>
<!-- no images found -->
<?php endif;
endwhile;
else :
// no images found
endif;
endwhile; ?>
</div>
解决方案
推荐阅读
- php - 使用 Ray Di 在构造函数中动态注入数据
- python - 在 python3 中导入 pwntools 时,回车 '\r' 停止工作
- package - Julia - 获取我的模块的版本号
- sql-server - Powershell Array Strings - 在 SSIS 中映射不同的变量参数名称
- python - 在 Python 中创建一个循环以从另一个列表中的一个列表中查找任何条目
- mysql - 在 docker 中使用 .net core 和 mysql 时出现数据库连接错误
- javascript - 如何在没有 sortable.js 的情况下使用 reactjs 对列表进行排序?
- c# - 在 MVC 中将 Crystal Reports 与 itext7 合并会从流中生成未合并的 pdf
- javascript - Prism.js 仅在开始行时突出显示
- android - 如何将 Flutter MethodChannel 添加到 Android Application.class?