2016-09-14 40 views

回答

1

最直接的方法是查詢該類別中的職位,像這樣:

// WP_Query arguments 
$args = array (
    'category_name'   => 'cars', 
    'posts_per_page'   => '-1', 
    'order'     => 'DESC', 
    'orderby'    => 'date', 
); 

// The Query 
$query = new WP_Query($args); 

然後你就可以得到的職位數與

// $query->found_posts gives the number of posts the query has found 
// with the parameters you set 
echo $query->found_posts; 

而且可以計數後您顯示:

$count = 0; 

foreach ($query->posts as $count_post) { 
    $count++; 
    // assuming you are inside The Loop 
    if (get_the_ID() == $count_post->ID) { 
     break; 
    } 
} 

// now you can get the "serial number" of the post 
echo $count; 

這可能不是做這件事的最「WP方式」,但它應該起作用。 :)

+0

謝謝,它真的可以工作:) – user3514052

+0

樂意幫忙! :d –

相關問題