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


8

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

乍看上去

有没有一种方法可以将“ 81 Wrestlers”文本变成指向帖子类型列表的链接。这是代码:

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;
}

Answers:


8

这是我用来在“概览”小部件中显示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>';
        }
    }
}

这使文本可单击为链接。希望这可以帮助


但是它显示了所有自定义帖子类型,我只想显示“ Wrestler”帖子类型。
Hardeep Asrani 2014年

我编辑了您的代码并将其与我的代码混合使用,它很有效:)谢谢!
Hardeep Asrani 2014年

@ Hardeep Asrani很高兴我可以提供帮助。如果您可以将代码添加为答案,那就太好了。
Pieter Goosen 2014年

@PieterGoosen可能是一个很长的镜头,但是我正在寻找“概览”框来显示正确的Dashicon(developer.wordpress.org/resource/dashicons)。任何的想法?
user2019515 '16

这确实有帮助...现在知道我如何用他们自己的图标显示它们吗?我正在尝试在输出中添加dashicon类$output = '<a class="' . $post_type->menu_icon . '" href="edit.php?post_type=' . $post_type->name . '">' . $num . ' ' . $text . '</a>';……但是有些样式可以覆盖它,所以我尝试添加此样式:#dashboard_right_now li a::before, #dashboard_right_now li > span::before { content: initial; }……但是可以覆盖dashicon类样式。请指教。
juliusbangert

2

好的,所以我使用此代码仅显示“摔跤手”帖子类型,并且可以正常工作。我混合了我和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;
}

0

在您发布的代码中,我无法真正理解以下几点:

if ( current_user_can( $post_type->cap->edit_posts ) ) {
  $items[] = sprintf( '%2$s', $type, $text ) . "\n";
} else {
  $items[] = sprintf( '%2$s', $type, $text ) . "\n";
}

IE浏览器是否当前用户可以编辑帖子类型做点什么,否则做同样的事情...

我想如果当前用户可以编辑帖子类型(就像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;
}

在我的情况下不起作用。
Hardeep Asrani 2014年

0

对于将来出现的所有事件,将自定义帖子类型添加到“概览”框中,以下代码在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;
        }
    }

所有功劳归以下作者

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.