我是wordpress的新手,正在学习寻呼帖子,但下面的代码不起作用,我找不到有什么问题。请帮忙。
the code:
<?php
$temp = $wp_query;
$wp_query= null;
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$wp_query = new WP_Query( \'posts_per_page=2&paged=\'.$paged );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<h2>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link( \'« Previous\' ); ?></div>
<div class="alignright"><?php next_posts_link( \'More »\' ); ?></div>
</div>
<?php
$wp_query = null;
$wp_query = $temp;
?>
the error
在/var/www/html/wordpress/wp includes/query中调用null上的成员函数get()。php在线28
最合适的回答,由SO网友:Sumit 整理而成
您正在设置null
到$wp_query
然后查询get_query_var( \'paged\' )
预计您将收到此错误!
第一次查询get_query_var( \'paged\' )
然后设置原始$wp_query
到null
示例:-
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
global $wp_query;
$temp = $wp_query;
$wp_query = new WP_Query( \'posts_per_page=2&paged=\'.$paged );
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<h2>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link( \'« Previous\' ); ?></div>
<div class="alignright"><?php next_posts_link( \'More »\' ); ?></div>
</div><?php
wp_reset_postdata();
$wp_query = $temp;
注意:不要忘记添加
wp_reset_postdata()
循环后。