我需要在添加到 Wordpress 侧边栏的滑块中显示属于类别的帖子的主图像,如果我在页面中,代码可以正常工作,但不在主页或搜索结果页面中,这是我的代码:
<ul id="sliderContent">
<?php
$joiners .= " LEFT OUTER JOIN wp_term_relationships r ON r.object_id = wp_posts.ID
LEFT OUTER JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id
LEFT OUTER JOIN wp_terms t ON t.term_id = x.term_id ";
$sentgeters .= " AND t.slug = 'CATEGORY_NAME_GOES_HERE' ";
$querystr = "
SELECT wp_posts.*
FROM wp_posts ".$joiners."
WHERE wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post' ".$sentgeters."
ORDER BY RAND() " ;
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<li class="sliderImage">
<a href="<?php the_permalink() ?>"><?php main_image(); ?></a>
<span class="top"><?php the_title(); ?></span>
</li>
<?php endforeach; ?>
<?php endif; ?>
<div class="clear sliderImage"></div>
</ul>
main_image() 是我编写的一个函数,用于显示上传的第一张图片。任何的想法?