在“一览表”元框中显示自定义帖子类型

时间:2014-03-22 作者:Hardeep Asrani

我发现以下代码片段将显示在Dashboard的概览小部件中发布的自定义帖子类型的数量,如下所示:

At A Glance

有没有办法将“81名摔跤手”文本链接到帖子类型列表。代码如下:

add_filter( \'dashboard_glance_items\', \'custom_glance_items\', 10, 1 );
function custom_glance_items( $items = array() ) {
    $post_types = array( \'wrestler\' );
    foreach( $post_types as $type ) {
        if( ! post_type_exists( $type ) ) continue;
        $num_posts = wp_count_posts( $type );
        if( $num_posts ) {
            $published = intval( $num_posts->publish );
            $post_type = get_post_type_object( $type );
            $text = _n( \'%s \' . $post_type->labels->singular_name, \'%s \' . $post_type->labels->name, $published, \'your_textdomain\' );
            $text = sprintf( $text, number_format_i18n( $published ) );
            if ( current_user_can( $post_type->cap->edit_posts ) ) {
                $items[] = sprintf( \'%2$s\', $type, $text ) . "\\n";
            } else {
                $items[] = sprintf( \'%2$s\', $type, $text ) . "\\n";
            }
        }
    }
    return $items;
}

4 个回复
最合适的回答,由SO网友:Hardeep Asrani 整理而成

好的,所以我用这个代码只显示“摔跤手”的帖子类型&;它成功了。我混合了矿山和;彼得·古森(Pieter Goosen)的代码:

add_filter( \'dashboard_glance_items\', \'custom_glance_items\', 10, 1 );
function custom_glance_items( $items = array() ) {
    $post_types = array( \'wrestler\' );
    foreach( $post_types as $type ) {
        if( ! post_type_exists( $type ) ) continue;
        $num_posts = wp_count_posts( $type );
        if( $num_posts ) {
            $published = intval( $num_posts->publish );
            $post_type = get_post_type_object( $type );
            $text = _n( \'%s \' . $post_type->labels->singular_name, \'%s \' . $post_type->labels->name, $published, \'your_textdomain\' );
            $text = sprintf( $text, number_format_i18n( $published ) );
            if ( current_user_can( $post_type->cap->edit_posts ) ) {
            $output = \'<a href="edit.php?post_type=\' . $post_type->name . \'">\' . $text . \'</a>\';
                echo \'<li class="post-count \' . $post_type->name . \'-count">\' . $output . \'</li>\';
            } else {
            $output = \'<span>\' . $text . \'</span>\';
                echo \'<li class="post-count \' . $post_type->name . \'-count">\' . $output . \'</li>\';
            }
        }
    }
    return $items;
}

SO网友:Pieter Goosen

下面是我用来在“概览”小部件中显示CPT的函数

add_action( \'dashboard_glance_items\', \'cpad_at_glance_content_table_end\' );
function cpad_at_glance_content_table_end() {
    $args = array(
        \'public\' => true,
        \'_builtin\' => false
    );
    $output = \'object\';
    $operator = \'and\';

    $post_types = get_post_types( $args, $output, $operator );
    foreach ( $post_types as $post_type ) {
        $num_posts = wp_count_posts( $post_type->name );
        $num = number_format_i18n( $num_posts->publish );
        $text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
        if ( current_user_can( \'edit_posts\' ) ) {
            $output = \'<a href="edit.php?post_type=\' . $post_type->name . \'">\' . $num . \' \' . $text . \'</a>\';
            echo \'<li class="post-count \' . $post_type->name . \'-count">\' . $output . \'</li>\';
        }
    }
}
这使得文本可以作为链接单击。希望这有帮助

SO网友:klewis

对于将来所有向“概览”框添加自定义帖子类型的情况,以下代码在WordPress 4.6.1中适用。这可能对其他人有用。

// Add custom taxonomies and custom post types counts to dashboard
    add_action( \'dashboard_glance_items\', \'cpt_to_at_a_glance\' );
function cpt_to_at_a_glance() {
        // Custom post types counts
        $post_types = get_post_types( array( \'_builtin\' => false ), \'objects\' );
        foreach ( $post_types as $post_type ) {
            $num_posts = wp_count_posts( $post_type->name );
            $num = number_format_i18n( $num_posts->publish );
            $text = _n( $post_type->labels->singular_name, $post_type->labels->name, $num_posts->publish );
            if ( current_user_can( \'edit_posts\' ) ) {
                $num = \'<li class="post-count"><a href="edit.php?post_type=\' . $post_type->name . \'">\' . $num . \' \' . $text . \'</a></li>\';
            }
            echo $num;
        }
    }
所有信用都归following author

SO网友:gmazzap

在您发布的代码中,我无法真正理解以下内容的意义:

if ( current_user_can( $post_type->cap->edit_posts ) ) {
  $items[] = sprintf( \'%2$s\', $type, $text ) . "\\n";
} else {
  $items[] = sprintf( \'%2$s\', $type, $text ) . "\\n";
}
也就是说,如果当前用户可以编辑帖子类型,请执行某些操作,否则请执行相同的操作。。。

如果当前用户可以编辑帖子类型(就像WordPress对页面和帖子所做的那样),我想您应该显示帖子列表的链接。

在这种情况下,您的代码变成:

function custom_glance_items( $items = array() ) {
  $post_types = array( \'wrestler\' );
  foreach( $post_types as $type ) {
    if( ! post_type_exists( $type ) ) continue;
    $num_posts = wp_count_posts( $type );
    if( $num_posts ) {
      $published = intval( $num_posts->publish );
      $post_type = get_post_type_object( $type );
      $text = _n(
        \'%s \' . $post_type->labels->singular_name,
        \'%s \' . $post_type->labels->name,
        $published,
        \'your_textdomain\'
      );
      $text = sprintf( $text, number_format_i18n( $published ) );

      // show post type list id user can edit the post type,
      // otherwise just swho the name and the count
      if ( current_user_can( $post_type->cap->edit_posts ) ) {
        $edit_url = add_query_arg( array(\'post_type\' => $type),  admin_url(\'edit.php\') );
        $items[] = sprintf( \'<a href="%s">%s</a>\', $edit_url, $text ) . "\\n";
      } else {
        $items[] = $text . "\\n";
      }

    } // end if( $num_posts )
  } // end foreach
  return $items;
}

结束

相关推荐

private functions in plugins

我开发了两个插件,其中一个功能相同(相同的名称,相同的功能)。当试图激活两个插件时,Wordpress会抛出一个错误,因为它不允许我以相同的名称定义函数两次。有没有一种方法可以使这个函数只对插件私有,而不使用面向对象编程,也不简单地重命名函数?我不想使用OOP,因为我首先要学习它。此外,我不想重命名该函数,因为我可能也想在其他插件中使用它,而重命名感觉不太合适。