2011-04-23 81 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

相关问题