我需要顯示從屬於類別的帖子中的主圖像添加到WordPress的側邊欄,代碼工作完美,如果我在一個頁面,但不是在主頁或搜索結果頁面,這是我的代碼: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()是我寫的函數,用於顯示上傳到帖子的第一張圖片。 有什麼想法?
您是否定義了'global $ wpdb'? –
你爲什麼要編寫自定義查詢?在wordpress中,建議使用get_posts或query_posts。 – 2013-09-28 10:18:20
@dianuj是的我做過 – housamz