0
我希望某個類別中的每個帖子都具有基於創建時間的增量值。將類別中的帖子設置爲遞增數字值作爲自定義字段
因此,在類別A中,第一個職位的數量爲1作爲自定義字段值,第二個數字2等。 如果我在類別B中創建了一個職位,那麼它也具有值1,如果我之後發佈職位類別C,它也有值1.
這是否清楚? :)
我發現這個片段,但它沒有考慮到類別問題。
function updateNumbers() {
/* numbering the published posts: preparation: create an array with the ID in sequence of publication date,/
/save the number in custom field 'incr_number' of post with ID/
/to show in post (within the loop) use <?php echo get_post_meta($post- >ID,'incr_number',true); ?>
/alchymyth 2010 */
global $wpdb;
$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 0 ;
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
$counts++;
add_post_meta($post->ID, 'incr_number', $counts, true);
update_post_meta($post->ID, 'incr_number', $counts);
endforeach;
endif;
}
add_action ('publish_post', 'updateNumbers');
add_action ('deleted_post', 'updateNumbers');
add_action ('edit_post', 'updateNumbers');
希望有人能幫助!