我正在使用这段代码来显示某个分类中的类别列表,以及该类别中的帖子数量。
每当我删除帖子时,帖子计数仍包含已删除的帖子并显示该类别标题,这也适用于已放入草稿的帖子。直到我永久删除帖子,类别和/或计数才会消失。
我错过了什么?
<?php $args = array(
'show_option_all' => '',
'orderby' => 'slug',
'order' => 'ASC',
'style' => 'list',
'show_count' => 1,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => 1,
'title_li' => __( '' ),
'show_option_none' => __('No categories'),
'number' => null,
'echo' => 0,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'artists',
'walker' => null
);
$variable = wp_list_categories($args);
$variable = preg_replace('/\<\/a\> \((.*)\)/',' <span class="quantity">$1</span></a>',$variable);
echo $variable;?>
答案 0 :(得分:1)
我做了一些谷歌搜索,在Wordpress表单上,我遇到了这段代码放入我的functions.php文件,虽然它似乎不允许我快速编辑,但它确实修复了帖子数量问题...
// Function that helps the post count
add_action('edited_term_taxonomy','yoursite_edited_term_taxonomy',10,2);
function yoursite_edited_term_taxonomy($term,$taxonomy) {
global $wpdb,$post;
//in quick edit mode, $post is an array()
//in full edit mode $post is an object
if ( is_array( $post ))
$posttype=$post['post_type'];
else
$posttype=$post->post_type;
if ($posttype) {
$DB_prefix=$wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
$sql = "UPDATE ".$DB_prefix."term_taxonomy tt
SET count =
(SELECT count(p.ID) FROM ".$DB_prefix."term_relationships tr
LEFT JOIN ".$DB_prefix."posts p
ON (p.ID = tr.object_id AND p.post_type = '".$posttype."' AND p.post_status = 'publish')
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id)
WHERE tt.taxonomy = '".$taxonomy->name."'
";
$wpdb->query($sql);
}
}
希望我能解释一下,但我对PHP很新......