当我需要这样的东西时,我会不时使用它:
<?php
/*
Plugin Name: List User Posts
Plugin URI: http://en.bainternet.info
Description: lists user posts on the front end
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/
if (!class_exists(\'list_user_posts\')){
/**
* list_user_posts
* @author Ohad Raz
*/
class list_user_posts
{
/**
* __construct class constructor
*
* @author Ohad Raz
* @param array $args
*/
function __construct($args = array())
{
add_shortcode(\'user_posts\', array($this,list_user_posts));
}
/**
* list_user_posts shortcode handler
*
* @author Ohad Raz
* @param array $attr shortcode attributes
* @param string $content shortcode content
* @return string
*/
public function list_user_posts($attr = array(), $content = null)
{
extract(shortcode_atts(array(
\'post_type\' => \'post\',
\'number\' => 10,
), $attr));
//if the user is not logged in the give him a link to log in
if (!is_user_logged_in()){
return sprintf(__(\'You Need to <a href="%s">Login</a> to see your posts\'),wp_login_url(get_permalink()));
}
//this is for pagination
$pagenum = isset( $_GET[\'pagenum\'] ) ? intval( $_GET[\'pagenum\'] ) : 1;
//get user\'s posts
$args = array(
\'author\' => get_current_user_id(), //this makes the query pull post form the current user only
\'post_status\' => array(\'draft\', \'future\', \'pending\', \'publish\'),
\'post_type\' => $post_type,
\'posts_per_page\' => $number,
\'paged\' => $pagenum
);
$user_posts = new WP_Query( $args );
$retVal = \'\';
if ( $user_posts->have_posts() ) {
//set table headers
$retVal = \'
<table class="user-posts-table" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>\'.__( \'Title\', \'lup\' ).\'</th>
<th>\'.__( \'Status\', \'lup\' ).\'</th>
<th>\'.__( \'Actions\', \'lup\' ).\'</th>
</tr>
</thead>
<tbody>\';
//loop over and add each post to the table
global $post;
$temp = $post;
while ($user_posts->have_posts()){
$user_posts->the_post();
$title = $post->post_title;
$link = \'<a href="\'.get_permalink().\'" title="\'.sprintf( esc_attr__( \'Permalink to %s\', \'lup\' ), the_title_attribute( \'echo=0\' ) ).\'" rel="bookmark">\'.$title.\'</a>\';
$retVal .=
\'<tr>
<td>
\'.( in_array( $post->post_status, array(\'draft\', \'future\', \'pending\') ) ? $title : $link).\'
</td>
<td>
\'.$post->post_status .\'
</td>
<td>
<a href="LINK_TO_YOUR_EDIT_PAGE"><span style="color: green;">\'. __( \'Edit\', \'lup\' ).\'</span></a>
<a href="LINK TO YOUR DELETE PAGE"><span style="color: red;">\'.__( \'Delete\', \'lup\' ).\'</span></a>
</td>
</tr>\';
}
$retVal .= \'</tbody></table>\';
//create pagination (if needed)
if ($user_posts->found_posts > $number ){
$pagination = paginate_links( array(
\'base\' => add_query_arg( \'pagenum\', \'%#%\' ),
\'format\' => \'\',
\'prev_text\' => __( \'«\', \'lup\' ),
\'next_text\' => __( \'»\', \'lup\' ),
\'total\' => $user_posts->max_num_pages,
\'current\' => $pagenum
)
);
if ( $pagination ) {
$retVal .= \'<div class="pagination">\'.$pagination .\'</div>\';
}
}
//return table of posts
return $retVal;
}else{
// no posts for this users found
return __("No Posts Found");
}
}
}//end list_user_posts class
}//end if
new list_user_posts();
到处都是评论,所以你可以了解那里发生了什么。
Usage 只需创建一个页面并添加以下短代码:[user_posts]
这将为您提供登录用户的帖子列表。
您也可以这样更改帖子类型:[user_posts post_type="deal" number="5"]