要列出一个用户提供的标题和永久链接的所有页面,您需要$wpdb->get_results()
. 以下代码基于此答案:How to count current user\'s pages?
首先,我们将计数器移动到一个单独的辅助函数中;我们以后可能还会需要它:
/**
* Get all post IDs and titles of a type for a user.
*
* @param int $user_id
* @param string $post_type \'page\' (default), \'post\', attachment, a custom post
* type or \'any\' (which excludes attachments)
* @return array
*/
function t5_user_pages( $user_id, $post_type = \'page\' )
{
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $user_id );
return $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts $where" );
}
现在,我们在代码中使用该函数:
if ( is_user_logged_in() )
{
$user = wp_get_current_user();
$results = t5_user_pages( $user->ID );
$count = count( $results );
if ( $count >= 1 )
{
print \'<h2>Hello \' . esc_html( $user->display_name ) . \'!</h2>
<p>These are your pages:</p>
<ul>\';
foreach ( $results as $result )
printf( \'<li><a href="%1$s">%2$s</a></li>\',
get_permalink( $result->ID ),
esc_html( $result->post_title )
);
print \'</ul>\';
} else {
// user is logged in but hasn\'t written anything
}
}
else
{
// user is not logged in
}