在循环中获取最新的作者帖子

时间:2013-10-08 作者:Robert

我有一个wordpress主题,需要检索最新的3篇作者帖子,这很简单,但我遇到了一个棘手的问题。

因为我已经在以下范围内进行了调用:

if ( have_posts() ) : while ( have_posts() ) :the_post();
endwhile; endif;
由于某种原因,它不起作用。

以下是我使用的代码:

if ( have_posts() ) : while ( have_posts() ) :the_post();

//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!
//some content goes here regarding the post itself!!!

$relatedargs = array(

\'author_name\' => get_the_author(),
\'post__not_in\' => array( $post->ID),
\'posts_per_page\' => 3

);

$relatedquery = new WP_Query( $relatedargs );

while($relatedquery->have_posts()){ $relatedquery->the_post(); 

?>

<div class="span3">

<?php
if(has_post_thumbnail()) { 
$relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'medium\', false);
$relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\', false);
?>

<div class="hover_colour"><a href="<?php echo $relatedthumbnail_large[\'0\']; ?>" rel="prettyPhoto"><img src="<?php echo $relatedthumbnail[\'0\']; ?>" alt="<?php the_title(); ?>" /></a></div>

<?php } ?>

<h6><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a><br><i class="icon-time muted"></i> <?php echo get_the_time(\'j\') . \'/\' . get_the_time(\'m\') . \'/\' . get_the_time(\'Y\') . \' \'; ?> <i class="icon-comments muted"></i> <a href="<?php the_permalink(); ?>"> <?php comments_number(0 . __(\' comments\',\'textdomain\'), 1 . __(\' comment\',\'textdomain\'), \'% \' . __(\'comments\',\'textdomain\')); ?></a></h6>

</div>

<?php } wp_reset_postdata(); ?>

endwhile; endif;
它在我的本地页面上百分之百工作,但当我上传到服务器时,它不会显示任何内容,我没有收到任何错误,我处于调试模式。

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

我认为你正在执行reset_postdata() 太早了,使用全局$post 对象错误地位于次循环内,并将错误的参数传递给次循环。试试这个。

在函数中放置函数以获取最新的作者帖子。php文件:

function the_latest_author_posts($post) {

        //some content goes here regarding the post itself!!!
        $relatedargs = array(

             \'author\' => $post->post_author,
             \'post__not_in\' => array( $post->ID),
             \'posts_per_page\' => 3

        );

        $relatedquery = new WP_Query( $relatedargs );

        while($relatedquery->have_posts()){
             $relatedquery->the_post(); 
             $ID = get_the_ID();
        ?>

             <div class="span3">

             <?php
                  if(has_post_thumbnail()) { 
                       $relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($ID), \'medium\', false);
                       $relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($ID), \'full\', false);
?>

                       <div class="hover_colour"><a href="<?php echo $relatedthumbnail_large[\'0\']; ?>" rel="prettyPhoto"><img src="<?php echo $relatedthumbnail[\'0\']; ?>" alt="<?php the_title(); ?>" /></a>
                       </div>

                 <?php } ?>

                       <h6><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a><br><i class="icon-time muted"></i> <?php echo get_the_time(\'j\') . \'/\' . get_the_time(\'m\') . \'/\' . get_the_time(\'Y\') . \' \'; ?> <i class="icon-comments muted"></i> <a href="<?php the_permalink(); ?>"> <?php comments_number(0 . __(\' comments\',\'textdomain\'), 1 . __(\' comment\',\'textdomain\'), \'% \' . __(\'comments\',\'textdomain\')); ?></a></h6>

               </div>

    <?php }
    wp_reset_postdata();
}
然后,在你的主题中,在循环中,你可以在任何你想显示当前帖子作者的最新帖子的地方调用该函数:

<?php
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        //some content goes here regarding the post itself!!!
        //some content goes here regarding the post itself!!!
        the_latest_author_posts($post);
    }
}
?>

结束

相关推荐

Get all posts without tags

我正在使用一个标签插件。有没有WordPress方法可以获取\\u帖子或查询所有没有标签的帖子?EDIT在提问时,我已经向WP Codex、search Stackexchange和Google查询了一个相关问题。我发现了一些有助于查找标记的结果,但不是NOT IN a中的运算符tax_query. 我还没有任何代码可以共享,因为我没有构建查询所需的信息$args.