Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t重置查询以在firs循环Wordpress后显示ramdom post_Php_Html_Wordpress - Fatal编程技术网

Php Can';t重置查询以在firs循环Wordpress后显示ramdom post

Php Can';t重置查询以在firs循环Wordpress后显示ramdom post,php,html,wordpress,Php,Html,Wordpress,我有一段代码,可以在一篇文章下面显示一篇随机的文章。但这段代码只显示了该帖子作者的文章。怎么了 <!-- post --> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php the_title(); ?> <?php echo get_avatar( get_the_author_meta( 'ID' )

我有一段代码,可以在一篇文章下面显示一篇随机的文章。但这段代码只显示了该帖子作者的文章。怎么了

    <!-- post -->
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 

        <?php the_title(); ?>

        <?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>
        <?php the_author(); ?>
        <?php the_category(none); ?>
        <?php the_date(); ?>


        <?php the_content();?>
        <?php $key="video"; echo get_post_meta($post->ID, $key, true); ?>
        <?php $key="imagen"; echo get_post_meta($post->ID, $key, true); ?>


    <?php endwhile; ?>

    <?php else : ?>

    <?php endif; ?>

    <!-- ramdom post -->

    <?php $posts = $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
       <?php the_post_thumbnail('photo-thumbnail') ?>
        <?php the_author(); ?>
        <?php the_category(none); ?>


    <?php } ?>

尝试在第一次查询后添加

用下面的代码替换随机邮政编码。在您的示例中,您查询了3篇文章。如果这不是你的意图,你只需要一篇文章,那么在每页的文章中用1篇替换3篇

$secondary_posts = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 3, 'no_found_rows' => 1, ) );
if ( $secondary_posts->have_posts() ) : while ( secondary_posts->have_posts() ) : $secondary_posts->the_post();

the_post_thumbnail( 'photo-thumbnail' );
the_author();
the_category();

endwhile; endif;

wp_reset_postdata();

我在之后尝试过,但不起作用:(你的“”没有函数,你应该删除它。重置查询的正确位置应该在endif之后。你的其余代码看起来很好,我不知道它为什么不能正常工作。我删除了“”并在endif之后添加,但不起作用:(这是网络,如果你在第二个循环中将“获取帖子”替换为“查询帖子”,可能会有帮助吗?我的sintax中有些地方不正确(我是初学者)页面未加载。这是带有更改的ramdom post的代码。我查看了JSFIDLE。您没有在PHP标记中包装endwhile或endif。添加这些标记,它应该可以正常工作。确保wp_reset_postdata也在PHP标记中。此外,在_post_缩略图函数后缺少分号。这应该不是问题,但不会rth整理它。如果您仍然有问题,请打开错误报告。我这样做了,但显示了一个白色页面->(ramdom代码部分)(整个页面代码)-->,这实际上可能是我的错误。我刚刚编辑了我的答案。您需要在If中的have_posts方法周围添加括号。因此If($secondary_posts->have_posts()):。。。