你只需要一张桌子class=\'widefat\'
和get_posts()
. 然后运行结果(如果有)并打印表行。
下面是一个非常简单的示例,作为仪表板小部件。请注意缺少I18n–它还没有准备好使用!
<?php
/**
* Plugin Name: Last posts table
* Plugin URI: http://wordpress.stackexchange.com/q/71887/73
*/
add_action( \'wp_dashboard_setup\', \'wpse_71887_register_dashboard_widget\' );
function wpse_71887_register_dashboard_widget()
{
wp_add_dashboard_widget(
__FUNCTION__,
\'Last Posts\',
\'wpse_71887_render_dashboard_widget\'
);
}
function wpse_71887_render_dashboard_widget()
{
$header = \'
<tr>
<th>User</th>
<th>Post</th>
<th>Time</th>
</tr>\';
print "<table class=\'widefat\'>
<thead>$header</thead>
<tfoot>$header</tfoot>
<tbody>";
// get 10 last private and published posts
$posts = get_posts(
array (
\'numberposts\' => 10,
\'post_type\' => array ( \'post\', \'page\' )
)
);
if ( ! $posts )
{
print \'<tr><td cols="3">No posts found. <a href="\'
. admin_url( \'post-new.php\' ) . \'">Write one!</a>\';
}
else
{
foreach ( $posts as $post )
{
printf(
\'<tr>
<td>%1$s</td>
<td><a href="%2$s">%3$s</a></td>
<td>%4$s ago</td>
</tr>\',
esc_html( get_user_by( \'id\', $post->post_author )->display_name ),
get_permalink( $post->ID ),
esc_html( $post->post_title ),
human_time_diff( mysql2date( \'U\', $post->post_date ) )
);
}
}
print \'</table>\';
}
结果