1
在category.php
,我想獲得該類別的帖子列表。Wordpress - get_posts()比The Loop更高效嗎?
我找到了兩種方法:使用get_posts($args)
和The Loop
。
get_posts()方式
$args = array (
"category" => ID);
$posts = get_posts($args);
// then use foreach to loop the $posts
的循環方式
<?php if (have_posts()): ?>
<?php while (have_posts()): the_post(); ?>
...
<?php endwhile; ?>
<?php endif; ?>
那麼,哪一個更有效?
從我在搜索中發現的,get_posts()
用於自定義模板,而The Loop
用於遵循WordPress命名約定的模板內。
我更喜歡get_posts()
,但是如果和The Loop相比有很大的開銷,我應該重新考慮一遍。