我有一個CMS設置客戶端與三個元框相關的內容。客戶所要做的就是發送一個頁面(每個頁面一個),網站將返回三個相關產品。WordPress獲取頁面通過ID返回奇怪的結果
一切工作正常,直到我的客戶不小心拼錯了其中一個slu。。 WordPress不返回任何內容,而是返回6個隨機項目。
IN的functions.php:
function get_ID_by_page_name($page_name) {
global $wpdb;
$page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."' AND post_type = 'page'");
return $page_name_id;
}
在模板文件:
$goesWith = get_post_meta($post->ID, 'goes_with', true);
if (($goesWith) or ($goesWith2) or ($goesWith3)) {
echo "<div class='goes-with'>";
echo "<h2>Goes Great With:</h2>";
// OPTION ONE
$pageID = get_ID_by_page_name($goesWith);
if ($goesWith) {
$args = array(
'post_type' => 'page',
'page_id' => $pageID,
'post_status' => 'publish'
);
query_posts($args);
while(have_posts()) {
the_post(); // vital
echo "<div class='post-product'>";
echo "<a href=";
the_permalink();
echo ">";
thumbPath(140, 140);
echo "</a><a href=";
the_permalink();
echo "><p>";
the_title();
echo "</p></a></div><!--end post-product-->";
}
}
else {
echo "";
}
wp_reset_query();
有關[query_posts vs WP_Query here]的更多信息(http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts ) – maiorano84