我已经使用WP\\u Query创建了自定义Wordpress循环,循环运行良好,我可以很好地自定义它,但是我在使用“博客页面最多显示”设置时遇到了问题,例如,如果我在我的循环中添加了以下参数\'post_per_page\' =>-1
我的循环显示所有帖子,但现在我无法在管理面板中插入自定义数量的帖子,如果我输入“博客页面最多显示”3(三个代表帖子数量),它们将被忽略,循环显示所有帖子
为了克服这个问题(因为我想控制显示在管理员阅读设置中的帖子数量)
我做了以下工作:创建一个空值变量,并将其作为如下参数传递:
$show_my_posts = \'\';
$args = array(
\'paged\' => $paged,
\'post_not_in\' => $show_my_posts
//added blank value variable in order to respect "Blog pages show at most" in backend
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
因此,现在,我可以在管理后端中插入自定义数量的帖子,我的循环按我所希望的方式运行(我输入“博客页面最多显示”,值为3),现在我的循环按我所希望的方式显示三篇帖子
我的问题是:
我这样做是“正确的方式”还是有更好的方式来实现这种行为 THX!!
我的完整循环如下所示:
<?php //enable pagination on static pages and blog pages
$show_my_posts = \'\';
$col = 1; //Let\'s create first column
/*Let\'s add pagination to post page and static page*/
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} elseif ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
$args = array(
/* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */
\'paged\' => $paged,
\'post_not_in\' => $show_my_posts
//added blank value variable in order to respect
//"Blog pages show at most" in backend
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($col == 1) echo \'<div class="row">\';//If column 1 create first row ?>
<?php if ($col == 2) echo \'<div class="row2">\';//If column 2 create second row ?>
<div <?php post_class(\'col\'.$col); ?> id="post-<?php the_ID(); ?>">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="entry">
<div class="featured_img">
<?php the_post_thumbnail();
echo \'<div class="featured_caption">\' . get_post(get_post_thumbnail_id())->post_excerpt . \'</div>\';?>
</div><!--/featured_img-->
<?php // let\'s enable more link on pages...
global $more;
$more = 0;
?>
<?php the_content(); ?>
<div class="clear"></div>
<div class="custom_fields"><?php the_meta(); ?></div><br/>
<p class="postmetadata">
<?php _e(\'Filed under:\',\'domain\'); ?> <?php the_category(\', \') ?> <?php _e(\'by\',\'domain\'); ?> <?php the_author(); ?><br/><?php the_tags(\'Tags:\', \', \', \'<br />\'); ?>
<?php _e(\'Posted on: \',\'domain\'); ?><?php the_time(\'l, F jS, Y\'); ?><br/>
<?php comments_popup_link(\'No Comments »\', \'1 Comment »\', \'% Comments »\'); ?> <?php edit_post_link(\'Edit\', \' | \', \'\'); ?>
</p>
</div>
</div>
<?php /*Enable Two Column Layout*/
if($col==1) {
$col=2;
echo "</div>";
}
else if($col==2) {
$col=1;
echo "</div>";
}
endwhile; ?>
<div class="clear"></div>
<div class="navigation">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, $paged ),
\'total\' => $wp_query->max_num_pages
) );
?>
</div>
<?php endif; ?>
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>