星期三 , 22 1 月 2025

新增管理面板小工具

add_action( 'wp_dashboard_setup', 'wp_learn_dashboard_widget' );


function wp_learn_dashboard_widget()
{
	wp_add_dashboard_widget( 'wp_learn_dashboard_widget', '最新添加文章', 'wp_learn_dashboard_widget_cb','wp_learn_dashboard_widget_control' );
}

function wp_learn_dashboard_widget_cb()
{
	$args = array(
		'numberposts'  => get_option( 'wp_learn_dashboard_widget_numberposts', 5 ),
		'post_status'  => 'publish',

	);

	$recent_posts = wp_get_recent_posts($args);


	echo '<ul>';

	foreach ($recent_posts as $recent_post) {
		echo '<li><a href="'.get_permalink($recent_post['ID']).'">'.$recent_post['post_title'].'</a></li>';
		
	}


	echo '</ul>';
}

function wp_learn_dashboard_widget_control()
{
	if (isset($_POST['wp_learn_dashboard_widget_numberposts'])) {

		$numberposts = sanitize_text_field( $_POST['wp_learn_dashboard_widget_numberposts'] );
		update_option( 'wp_learn_dashboard_widget_numberposts', $numberposts );
		
	}
	echo '<label>Enter the number of posts</label>';

	echo '<input type="text" value="'.get_option( 'wp_learn_dashboard_widget_numberposts', 5 ).'" name="wp_learn_dashboard_widget_numberposts" />';
}