php - Wordpress 在显示每个类别的帖子时具有多个循环,我可以让它更快吗?
问题描述
所以我正在使用 wordpress 和自定义主题。在登录页面中,我想显示每个类别的帖子。
示例:假设我有猫、狗、驴三个类别。然后在我的登陆页面上,我会有类似的东西:
- 最新10只猫
- 最新的10只狗
- 最新的 10 头驴
为此,我最终做了三个循环(也许更多的类别更多),所以基本上我最终写了这样的东西:
<?php $posts = query_posts( array ( 'category_name' => 'cats', 'order' => 'ASC', 'posts_per_page' => 10 ) );?>
<h1>my cats</h1>
<?php
foreach ($posts as $post) :
if ($post):
?>
<!-- displaying info not so important -->
<div class="some_wrapper">
<a href="<?php the_permalink();?>">
<img <?php $turl = get_the_post_thumbnail_url(); echo ($turl !== false) ? 'src="' . $turl . '"' : 'src="https://i.picsum.photos/id/10/800/600.jpg"'; ?> alt=""/>
<span><?php echo get_the_title(); ?></span></a>
</div>
<?php endif;endforeach; ?>
<?php $posts = query_posts( array ( 'category_name' => 'dogs', 'order' => 'ASC', 'posts_per_page' => 10 ) );?>
<h1>my dogs</h1>
<?php
foreach ($posts as $post) :
if ($post):
?>
<!-- displaying info not so important -->
<div class="some_wrapper">
<a href="<?php the_permalink();?>">
<img <?php $turl = get_the_post_thumbnail_url(); echo ($turl !== false) ? 'src="' . $turl . '"' : 'src="https://i.picsum.photos/id/10/800/600.jpg"'; ?> alt=""/>
<span><?php echo get_the_title(); ?></span></a>
</div>
<?php endif;endforeach; ?>
<?php $posts = query_posts( array ( 'category_name' => 'donkeys', 'order' => 'ASC', 'posts_per_page' => 10 ) );?>
<h1>my donkeys</h1>
<?php
foreach ($posts as $post) :
if ($post):
?>
<!-- displaying info not so important -->
<div class="some_wrapper">
<a href="<?php the_permalink();?>">
<img <?php $turl = get_the_post_thumbnail_url(); echo ($turl !== false) ? 'src="' . $turl . '"' : 'src="https://i.picsum.photos/id/10/800/600.jpg"'; ?> alt=""/>
<span><?php echo get_the_title(); ?></span></a>
</div>
<?php endif;endforeach; ?>
一切正常,但是, 我可以让它更快吗,比如做一个循环而不是 3 个(或者更多,如果你有更多的类别)或者使用另一种循环方式?
解决方案
推荐阅读
- python - re.sub() 通过多行替换
- html - 使用 css 选择器从所有行中选择第一个 td
- html - 在 matToolTip (Angular) 中呈现 html 内容
- c# - 如何仅从用户输入中侦听 DateTimePicker ValueChanged 事件
- java - 如何使用 mp4parser 获取 FPS?
- dialogflow-es - 在 Java 中实现 Dialogflow v2 webhook 的最佳方法是什么
- javascript - 如何使用 VS Code 设置字符串的智能感知以避免在 JavaScript 中键入无效值
- javascript - Jquery如何验证特定字符的输入并遵循特定模式
- nlp - 语言模型评分句子?
- php - PHP Timestampable - 更新和创建一个字段,可能吗?