按顺序调用帖子并为每个帖子分配不同的变量(wordpress)

发布于 2025-01-13 09:46:26 字数 2359 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2025-01-20 09:46:26

是的,我只是重构你的代码:

$icons     = [
  '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>',
 ];

 $query = new WP_Query( [
  'post_type'      => 'content',
  'post_status'    => 'publish',
  'category_name'  => 'service',
  'posts_per_page' => 4,
  'order'          => 'ASC',
 ] );

if ($query->have_posts()) {
  $counter = 1;
  while ($query->have_posts()) {
      $query->the_post();
      echo sprintf('<li>%s <h4>%s</h4> %s</li>',
                        has_post_thumbnail() ? get_the_post_thumbnail() : 
                                               $icons['icon_' . $counter],
                        get_the_title(), 
                        get_the_content()
      );
      $counter++;
}

Yes, I just refactor your code :

$icons     = [
  '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>',
 ];

 $query = new WP_Query( [
  'post_type'      => 'content',
  'post_status'    => 'publish',
  'category_name'  => 'service',
  'posts_per_page' => 4,
  'order'          => 'ASC',
 ] );

if ($query->have_posts()) {
  $counter = 1;
  while ($query->have_posts()) {
      $query->the_post();
      echo sprintf('<li>%s <h4>%s</h4> %s</li>',
                        has_post_thumbnail() ? get_the_post_thumbnail() : 
                                               $icons['icon_' . $counter],
                        get_the_title(), 
                        get_the_content()
      );
      $counter++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文