如何按类别和日期对帖子进行编号?

时间:2016-07-22 23:59:34

标签: php wordpress

此功能按照日期对帖子进行压缩,我也希望按类别添加,因此来自“自然”的帖子有自己的号码,“城市”的帖子也有自己的编号。

function updateNumbers() {
/* numbering the published posts, starting with 1 for oldest;
/ creates and updates custom field 'incr_number';
/ 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' 
             ORDER BY $wpdb->posts.post_date ASC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 0 ;
if ($pageposts):
foreach ($pageposts as $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', 11 );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );

我通过

调用该函数
<?php echo get_post_meta($post->ID,'incr_number',true); ?>

0 个答案:

没有答案