有没有办法获取 Wordpress 帖子,以便元调用实际上不进行数据库调用?
例如,我目前有:
$args = array(
'posts_per_page' => 1000,
'post_type' => 'portfoliosample'
);
$query = new WP_Query($args);
while($query->have_posts()):
$query->the_post();
$style = '';
if(has_post_thumbnail($post->ID)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$largeurl = $image[0];
$style = ' style="background-image:url('.$largeurl.');"';
}
?>
<li id="post-<?php the_ID(); ?>" <?php post_class('half'); ?>>
<a href="<?php the_permalink()?>"<?php echo $style?> ng-click="lightbox('portfolio','<?php the_permalink()?>')">
<h3 class="entry-title caps"><?php echo strtoupper(the_title('', '', false)); ?></h3>
</a>
</li>
<?php
endwhile;
诸如the_title()
不要进行数据库调用之类的事情,因为post
有该信息。但是,$post
似乎没有帖子的缩略图 ID 或该缩略图的图像源。
如果我执行这样的自定义查询:
global $wpdb;
$query = "
SELECT posts.*
FROM $wpdb->posts posts
WHERE posts.post_type = 'portfoliosample'
AND posts.post_status = 'publish'
ORDER BY posts.menu_order ASC
";
$pageposts = $wpdb->get_results($query, OBJECT);
if($pageposts)
{
global $post;
foreach($pageposts as $post)
{
setup_postdata($post);
$style = '';
if(has_post_thumbnail($post->ID)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$largeurl = $image[0];
$style = ' style="background-image:url('.$largeurl.');"';
}
?>
<li id="post-<?php the_ID(); ?>" <?php post_class('half'); ?>>
<a href="<?php the_permalink()?>"<?php echo $style?> ng-click="lightbox('portfolio','<?php the_permalink()?>')">
<h3 class="entry-title caps"><?php echo strtoupper(the_title('', '', false)); ?></h3>
</a>
</li><!-- #post-## -->
<?php
}
}
我可以同时获取元数据但仍然允许类似的功能get_post_thumbnail()
吗?我希望setup_postdata()
有办法处理这个问题。