image - 如何在具有帖子类型的自定义页面上显示所有图像字段
问题描述
我正在尝试遍历 ACF 的字段以显示帖子类型和特定类别的所有图像
我举一个实际的例子:我有一个 ACF 图像字段(image_of_project),我想查看与 XYZ 类别关联的帖子类型(项目)中上传的所有图像
现在我创建了这个循环:
'
$loop = new WP_Query( array(
'post_type' => 'projects',
'posts_per_page' => 9,
) );
while ( $loop->have_posts() ) : $loop->the_post();
$images = get_field('image_of_project');
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<div class="item">
<a href="<?php echo get_permalink( $post->ID ); ?>" target="_blank">
<?php echo wp_get_attachment_image( $image ); ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
'
图像已显示,但我不明白为什么它会重复我每张图像,而不是只显示链接到特定类别的图像。
感谢任何将帮助我完成这项任务的人!
解决方案
对于那些对该功能感兴趣的人,这就是它对我的工作方式。
函数.php
// Loop Gallery Cat1
function my_custom_gallery_cat1() {
query_posts(array(
'post_type' => 'gd_project',
'orderby' => 'rand',
'posts_per_page' => 10,
'tax_query' => array(
array (
'taxonomy' => 'gd_projectcategory',
'field' => 'slug',
'terms' => 'example-terms',
)
),
));
?> <div class="masonry">
<?php
while (have_posts()) : the_post();
$image_field = get_field('image');
$image_url = $image_field['url'];
$image_alt = $image_field['alt']; ?>
<div class="grid-item">
<a href="<?php echo get_permalink( $post->ID ); ?>" target="_blank">
<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($image_alt); ?>" />
<h4><?php the_field( 'title' ); ?></h4>
</a>
</div>
<?php endwhile; ?>
</div> <?php
}
add_shortcode('example-terms', 'my_custom_gallery_cat1');
单例.php
<div class="container-fluid">
<div class="row">
<?php echo do_shortcode('[categoria]');?>
</div>
</div>
推荐阅读
- tensorflow - 使用 Tensor 模型实现 yolo 模型
- php - 我是否为标签使用了不正确的正则表达式字符串?
- arrays - Julia:接受多种数组类型的模板函数
- r - 是否可以使用 ggplot2 在散点图上定位星形字形?
- abap - 捕捉 SUBMIT 语句的错误
- perl - perl 模块 Class::HPLOO v0.23 安装问题
- arrays - 给定 4 个未排序的数组,用 sum 找到所有可能的四元组
给定四个长度相等的数组,它们没有排序,它们有唯一的元素,但是数组之间的元素可能会发生冲突,所以我们被要求从每个数组中选择一个元素并满足这个条件 "x1+x2+x3+x4 < m"并计算所有可能的解决方案。
我们可以做一些比排序所有数组并在 O(n^3*logn) 中做更好的事情吗?
- reactjs - 使用商店 Mocha 和 Enzyme 测试组件
- java - 异常处理器后的骆驼中止路线
- xml - XSL - 从其他结构替换节点值但名称相同