按顺序调用帖子并为每个帖子分配不同的变量(wordpress)
我是 WordPress 新手,
我有四个变量和四个帖子,
我想按顺序将每个图标作为缩略图分配给每个帖子,
有什么方法可以在不重复功能的情况下分配它们?
这是我的代码:
<?php
$icons = array(
'icon_1' => '<picture><i class="fad fa-hourglass-end fa-3x"></i></picture>',
'icon_2' => '<picture><i class="fad fa-shield fa-3x"></i></picture>',
'icon_3' => '<picture><i class="fad fa-hand-holding-water fa-3x"></i></picture>',
'icon_4' => '<picture><i class="fad fa-hands-helping fa-3x"></i></picture>',
);
$args = array(
'post_type' => 'content',
'post_status' => 'publish',
'category_name' => 'service',
'posts_per_page' => 4,
'order' => 'ASC',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<li>
<?php
if ( has_post_thumbnail() ) :
?>
<picture>
<?php the_post_thumbnail(); ?>
</picture>
<?php
else :
if ( has_post_thumbnail() == false) {
foreach ($icons as $key => $value) {
return $value;
}
}
endif
?>
<span></span>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</li>
<?php
endwhile;
endif;
?>
Im new to wordpress
I have four variables and four posts
I want assign each icon as thumbnail to each post by order,
is there any ways to assign them without duplicate the function?
here is my code:
<?php
$icons = array(
'icon_1' => '<picture><i class="fad fa-hourglass-end fa-3x"></i></picture>',
'icon_2' => '<picture><i class="fad fa-shield fa-3x"></i></picture>',
'icon_3' => '<picture><i class="fad fa-hand-holding-water fa-3x"></i></picture>',
'icon_4' => '<picture><i class="fad fa-hands-helping fa-3x"></i></picture>',
);
$args = array(
'post_type' => 'content',
'post_status' => 'publish',
'category_name' => 'service',
'posts_per_page' => 4,
'order' => 'ASC',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<li>
<?php
if ( has_post_thumbnail() ) :
?>
<picture>
<?php the_post_thumbnail(); ?>
</picture>
<?php
else :
if ( has_post_thumbnail() == false) {
foreach ($icons as $key => $value) {
return $value;
}
}
endif
?>
<span></span>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</li>
<?php
endwhile;
endif;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我只是重构你的代码:
Yes, I just refactor your code :