問題描述
儀錶板方便地告訴我有多少帖子,頁面和評論。我想告訴我有多少文章,影片和漫畫 (三個自定義的帖子型別註冊我的主題) 。我將如何將其包含在儀錶板上的”Right Now” 面板中?
最佳解決方案
是的,該小工具中有幾個操作,包括 right_now_content_table_end 。例:
function my_right_now() {
$num_widgets = wp_count_posts( 'widget' );
$num = number_format_i18n( $num_widgets->publish );
$text = _n( 'Widget', 'Widgets', $num_widgets->publish );
if ( current_user_can( 'edit_pages' ) ) {
$num = "<a href='edit.php?post_type=widget'>$num</a>";
$text = "<a href='edit.php?post_type=widget'>$text</a>";
}
echo '<tr>';
echo '<td class="first b b_pages">' . $num . '</td>';
echo '<td class="t pages">' . $text . '</td>';
echo '</tr>';
}
add_action( 'right_now_content_table_end', 'my_right_now' );
次佳解決方案
基於相同的程式碼,您可以使用它來顯示每個自定義帖子型別和自定義分類數:
// Add custom taxonomies and custom post types counts to dashboard
add_action( 'right_now_content_table_end', 'my_add_counts_to_dashboard' );
function my_add_counts_to_dashboard() {
// Custom taxonomies counts
$taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' );
foreach ( $taxonomies as $taxonomy ) {
$num_terms = wp_count_terms( $taxonomy->name );
$num = number_format_i18n( $num_terms );
$text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name, $num_terms );
$associated_post_type = $taxonomy->object_type;
if ( current_user_can( 'manage_categories' ) ) {
$num = '<a href="edit-tags.php?taxonomy='%20.%20$taxonomy->name%20.%20'&post_type='%20.%20$associated_post_type%5B0%5D%20.%20'">' . $num . '</a>';
$text = '<a href="edit-tags.php?taxonomy='%20.%20$taxonomy->name%20.%20'&post_type='%20.%20$associated_post_type%5B0%5D%20.%20'">' . $text . '</a>';
}
echo '<td class="first b b-' . $taxonomy->name . 's">' . $num . '</td>';
echo '<td class="t ' . $taxonomy->name . 's">' . $text . '</td>';
echo '</tr><tr>';
}
// 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 = '<a href="edit.php?post_type='%20.%20$post_type->name%20.%20'">' . $num . '</a>';
$text = '<a href="edit.php?post_type='%20.%20$post_type->name%20.%20'">' . $text . '</a>';
}
echo '<td class="first b b-' . $post_type->name . 's">' . $num . '</td>';
echo '<td class="t ' . $post_type->name . 's">' . $text . '</td>';
echo '</tr>';
if ( $num_posts->pending > 0 ) {
$num = number_format_i18n( $num_posts->pending );
$text = _n( $post_type->labels->singular_name . ' pending', $post_type->labels->name . ' pending', $num_posts->pending );
if ( current_user_can( 'edit_posts' ) ) {
$num = '<a href="edit.php?post_status=pending&post_type='%20.%20$post_type->name%20.%20'">' . $num . '</a>';
$text = '<a href="edit.php?post_status=pending&post_type='%20.%20$post_type->name%20.%20'">' . $text . '</a>';
}
echo '<td class="first b b-' . $post_type->name . 's">' . $num . '</td>';
echo '<td class="t ' . $post_type->name . 's">' . $text . '</td>';
echo '</tr>';
}
}
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
