2011-04-23 79 views

回答

1

雖然這個職位給你一個良好的開端,這裏要說的是,我會用

// Set the desired category 
$category = 1; 

// Make query for posts in the category 
$my_query = new WP_Query(); 
$my_query->query(
    array(
     'cat' => $category, 
     // Does not show sticky posts; use 'caller_get_posts' if using < WP 3.1 
     'ignore_sticky_posts' => 1 
    ) 
); 

// Make sure some posts were found. 
if($my_query->have_posts()) 
{ 
    // Loop through each post found. 
    while($my_query->have_posts()) 
    { 
     // Setup the post data to use 
     $my_query->the_post(); 
     global $post; 

     // Echo out the title; Note that no formatting has been done 
     the_title(); 
    the_content();      
    } 
} 

現在的代碼,你也可以拿到冠軍,或者:

$title = get_the_title($post->ID); 
$title = $post->post_title; 

此外,還可以獲取帖子內容:

$content = $post->post_content; 

此外,您可以使用這些參數中的任何一個來獲取類別:

cat (int) - use category id. 
category_name (string) - use category slug (NOT name). 
category__and (array) - use category id. 
category__in (array) - use category id. 
category__not_in (array) - use category id. 

更多關於WP_Query類可以在這裏找到:http://codex.wordpress.org/Function_Reference/WP_Query

+0

什麼在$類別VAR寫? :-s我需要自己動態地找到它: -/ – william 2011-04-23 22:39:06

+0

不應該是一個問題。在什麼情況下你會使用這段代碼?類別ID很可能被找到,我只需要知道您何時可以執行代碼來幫助您找出如何找到類別ID。 – tollmanz 2011-04-23 23:02:48

+0

我在我的主題中使用category.php來顯示數據。 Category.php代碼與您的代碼。 http://pastie.textmate.org/private/gd0umxohmpixugaf3mbpdw 而鏈接來自sidebar.php,看起來像: http://pastie.textmate.org/private/qvdwvoe5leb6j1hveimhxw – william 2011-04-24 11:12:45

相關問題