如何在特定页面中显示用户自己的帖子

时间:2012-09-09 作者:ashishjain

我为帖子创建了一个页面,并在中将默认帖子页面更改为该页面Settings > Reading. 注册站点时,默认的新用户角色是subscriber。

问题是在登录后单击diaryview 页面显示其他用户的所有帖子,但我只想在diaryview 发布页面。

如何在此页面上仅查看我的帖子?

2 个回复
SO网友:Douglas.Sesar

尝试使用wp\\u查询

以下是codex链接:

WP_Query - Docs - WordPress

下面是一个可能有用的示例:

global $current_user;
      get_currentuserinfo();
$authorID = $current_user->ID;
    $args = array(
              \'post_type\' => \'post\',//or whatever you need
              \'posts_per_page\' => 5,
              \'author\' => $authorID
              );

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query($args);
您可以在自己的模板页面中使用此选项仅显示当前用户的帖子。

SO网友:Michael

这对我有用。注释掉post\\u优先级

<div class="leftdash">
<h2>My Post</h2>
<?php
 $current_user = wp_get_current_user();
$args = array(
\'author\'        =>  $current_user->ID,
\'orderby\'       =>  \'post_date\',
\'order\'         =>  \'ASC\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1
);

$current_user_posts = get_posts( $args );
//echo "<pre>"; print_r($current_user_posts); die; 
foreach ($current_user_posts as $post) {
?>

    <article id="post-<?php echo $post->ID; ?>" class="post-814 post type-post status-publish format-standard hentry category-uncategorized tag-priority-6">
<header class="page-header">
        <h1 class="page-title"><a href="<?php echo $post->guid; ?>" rel="bookmark"><?php echo $post->post_title; ?></a></h1>
                <div class="entry-meta">
                </div><!-- .entry-meta -->
        </header><!-- .entry-header -->
    <div class="entry-content">
        <p><?php $content = $post->post_content; 
            $trimmed_content = wp_trim_words( $content, 5, NULL );
            echo $trimmed_content
        ?></p>            
     </div><!-- .entry-content -->
<footer class="entry-meta">
    <span>Priority <?php  echo get_field( "post_priority", $post->ID ); ?></span>
     <span class="cat-links">
            Posted in <?php $category_detail=get_the_category($post->ID);//$post->ID
                foreach($category_detail as $cd){
                 ?>
                        <a  href="<?php echo get_category_link($cd->cat_ID); ?> ">
                        <?php echo $cd->cat_name; ?> </a>

                 <?php
                } ?>
             </span>

                    <span class="tags-links">
            Tagged <?php $postid = $post->ID;
                $posttags =  get_the_tags($postid); 
                if ($posttags) {
                foreach($posttags as $tag) {
                ?>
                     <a href="<?php echo get_tag_link($tag->term_id); ?>" rel="tag"><?php echo $tag->name ; ?></a>            

                <?php } } ?>
                </span>

            <span class="comments-link"><a href="<?php echo get_comments_link( $post->ID ); ?>">Leave a comment</a></span>
            <span class="edit-link"><a class="post-edit-link" href="<?php echo get_edit_post_link($post->ID); ?>">Edit</a></span>

    </footer><!-- .entry-meta -->

结束

相关推荐

POSTS_PER_PAGE选项限制库项目的数量

我注意到,如果我有一个将posts\\u per\\u page设置为4的查询,那么属于其中一篇文章的任何(图像)库也将只有4个项目(尽管当我从Add new post创建库时,库中有10个图像)。我怎样才能解决这个问题?