admin管理员组文章数量:1203458
I'm trying to solve a problem I have for many days. I'm posting it here in case some of you would know the answer.
I want to display a grid of 9 posts on my homepage. Each of these posts has one custom taxonomy apply to them. It's their issue name.
If one of the posts doesn't have a featured image, I want the issue image to appear. I added a field with ACF to be able to have a picture for my custom taxonomy.
Thanks in advance for your help.
François
<?php
$args = [
"posts_per_page" => 9,
"post_type" => "post",
"post_status" => "publish",
"order" => "ASC",
];
$query = new WP_Query($args);
if ($query->have_posts()): ?>
<section class="home-grid">
<?php while ($query->have_posts()):
$query->the_post(); ?>
<div>
<div class="pb-3">
<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail("article-large"); ?>
</a>
<?php else:$terms = wp_get_post_terms($post->ID, "issues");
foreach ($terms as $cat):
$size = "article-large";
$image_id = get_field("image_tag", $cat, false);
$image = wp_get_attachment_image_src($image_id, $size);
?>
<img src="<?php echo $image[0]; ?>" />
<?php endforeach;endif; ?>
</div>
<div class="px-1">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="author-link">
<?php $confirm = get_field("other_type_of_byline"); ?>
<h4><?php the_field("other_type_of_byline"); ?></h4>
</div>
<div class="read-more"><a href="<?php the_permalink(); ?>">
<span class="text">read</span><span class="icon">
<svg xmlns="; viewBox="0 0 32 32" style="width:24px;height:24px;margin-left:5px;fill:#F46767"><path d="M 21.1875 9.28125 L 19.78125 10.71875 L 24.0625 15 L 4 15 L 4 17 L 24.0625 17 L 19.78125 21.28125 L 21.1875 22.71875 L 27.90625 16 Z"></path></svg>
</span></a></div>
</div>
</div>
<?php
endwhile; ?>
</section>
<?php endif;
wp_reset_postdata();
?>
I'm trying to solve a problem I have for many days. I'm posting it here in case some of you would know the answer.
I want to display a grid of 9 posts on my homepage. Each of these posts has one custom taxonomy apply to them. It's their issue name.
If one of the posts doesn't have a featured image, I want the issue image to appear. I added a field with ACF to be able to have a picture for my custom taxonomy.
Thanks in advance for your help.
François
<?php
$args = [
"posts_per_page" => 9,
"post_type" => "post",
"post_status" => "publish",
"order" => "ASC",
];
$query = new WP_Query($args);
if ($query->have_posts()): ?>
<section class="home-grid">
<?php while ($query->have_posts()):
$query->the_post(); ?>
<div>
<div class="pb-3">
<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail("article-large"); ?>
</a>
<?php else:$terms = wp_get_post_terms($post->ID, "issues");
foreach ($terms as $cat):
$size = "article-large";
$image_id = get_field("image_tag", $cat, false);
$image = wp_get_attachment_image_src($image_id, $size);
?>
<img src="<?php echo $image[0]; ?>" />
<?php endforeach;endif; ?>
</div>
<div class="px-1">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="author-link">
<?php $confirm = get_field("other_type_of_byline"); ?>
<h4><?php the_field("other_type_of_byline"); ?></h4>
</div>
<div class="read-more"><a href="<?php the_permalink(); ?>">
<span class="text">read</span><span class="icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" style="width:24px;height:24px;margin-left:5px;fill:#F46767"><path d="M 21.1875 9.28125 L 19.78125 10.71875 L 24.0625 15 L 4 15 L 4 17 L 24.0625 17 L 19.78125 21.28125 L 21.1875 22.71875 L 27.90625 16 Z"></path></svg>
</span></a></div>
</div>
</div>
<?php
endwhile; ?>
</section>
<?php endif;
wp_reset_postdata();
?>
Share
Improve this question
edited Mar 10, 2022 at 17:07
François
asked Mar 10, 2022 at 15:56
FrançoisFrançois
274 bronze badges
3
- can you fix the indenting of your code? It makes it very difficult to work with or read – Tom J Nowell ♦ Commented Mar 10, 2022 at 16:16
- @TomJNowell Thanks for the comment. I just changed it. – François Commented Mar 10, 2022 at 16:20
- I'm not seeing fixed indenting, it looks like you removed all indenting and flattened it, which is also difficult to read as now all the structure is hidden, take a look at codehs.gitbooks.io/introcs/content/Programming-with-Karel/… – Tom J Nowell ♦ Commented Mar 10, 2022 at 16:37
1 Answer
Reset to default 0So, I finally found the solution. Maybe there are better ways to arrive to the same result, but at least, it works.
Here it goes:
<?php
$terms = get_the_terms( $post->ID , 'issues' );
foreach ($terms as $key) :
$size = "article-small";
$image_id = get_field("image_tag", $key, false);
$image = wp_get_attachment_image_src($image_id, $size);
?>
<img src="<?php echo $image[0]; ?>" />
<?php endforeach; ?>
本文标签: custom taxonomyDisplay term picture of each post in a loop
版权声明:本文标题:custom taxonomy - Display term picture of each post in a loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738668509a2105850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论