0

我正在使用这段代码来显示某个分类中的类别列表,以及该类别中的帖子数量。

每当我删除帖子时,帖子计数仍包括已删除的帖子并显示该类别标题,这也适用于已放入草稿的帖子。直到我永久删除该类别和/或计数消失的帖子。

我错过了什么?

<?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;?>
4

1 回答 1

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 很陌生......

于 2013-10-17T21:13:52.430 回答