Php Can';我不能让这个过于复杂的循环正常工作
因此,我正在我的WP主题文件夹中编辑index.php,基本上我希望它做的是:Php Can';我不能让这个过于复杂的循环正常工作,php,wordpress,loops,posts,if-statement,Php,Wordpress,Loops,Posts,If Statement,因此,我正在我的WP主题文件夹中编辑index.php,基本上我希望它做的是: 在博客的第一页显示4篇文章 第一页上的第一篇(最新)文章的风格有所不同。其他3个样式相同 每隔一页显示6篇文章 这是我正在使用的循环,第一页看起来很好,但是由于某些原因,在第2页和更高的页面上,它执行起来非常奇怪,并且在每个附加页面上只显示一篇文章。此外,它将只显示标题,而不显示日期或摘录。这是我的密码: <?php if( is_home() &&a
- 在博客的第一页显示4篇文章
- 第一页上的第一篇(最新)文章的风格有所不同。其他3个样式相同
- 每隔一页显示6篇文章
<?php
if( is_home() && !is_paged() ){
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
if (++$counter == 1) { ?>
<div class="featured_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('featuredblog'); ?></a>
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="regular_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<div class="postimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('regularblog'); ?></a></div>
<a href="<?php the_permalink(); ?>"><h3><?php
// short_title($after, $length)
echo short_title('...', 7);
?></h3></a>
<?php the_excerpt(); ?>
</div>
<?php } ?>
<?php endwhile;
else :
// Code for no posts found here
endif;
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args); ?>
<div class="regular_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<div class="postimage"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('regularblog'); ?></a></div>
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<?php the_excerpt(); ?>
</div>
<?php } ?>
可能里面的if…else语句太多了?您使用的是某种框架吗?这是一个相当糟糕的坦帕尔特引擎 无论如何,如果你需要添加样式,你应该把工作留给CSS 如果您需要非常高的精度,您可以在类中为每个需要不同样式的元素保留一个计数器
就像
我的建议会遵循是123,但实际上这一切都留给css。基本上,所有的帖子都应该包装在一个父div中,并且每个帖子都应该以相同的方式进行分类
<div id="content">
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
</div>
这就是你应该解决的问题,显然,文章中有更多的标签
从这里开始,可以使用#content>.post:first child
选择器来改变第一篇文章的样式
此外,每页有不同数量的帖子可能不是一个好主意。实际上,我从来没有真正尝试过,但我可以看到哪里的寻呼会出错,你可能永远也看不到第5或第6篇文章。我想wordpress可能会说,每页6篇文章,第2页应该总是从第7篇文章开始。但不是正面的。
<?php if (is_home() && !is_paged() ) {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args);
} ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="post<?php echo ++$counter; ?>">
<?php if ( is_home() && !is_paged() && $counter == 1 ) { ?>
do stuff
<?php } else { ?>
do other stuff
<?php } ?>
</div>
<?php endwhile; ?>
他使用的是wordpress,也就是wordpress模板引擎。我想你会发现很多人不同意你对它的厌恶后端代码和插件的结构非常好。模板引擎可以非常干净。我想你会发现任何工具都可能被误用。现在去挑选一个答案,因为50%太低了,所以,我找到了答案。见下文。我理解CSS的力量,并意识到我可以这样做。唯一的问题是,我不能在第一页上显示4个,在下一页上显示6个。@nurain,我告诉你,我认为你无法做到这一点,也无法拥有一个功能正常的网站。这就是我最后一段的内容。如果你知道你可以用css来做,那就去做吧。它会显著地清理你的代码,也许你会想出一些神奇的方法在wordpress中翻页。
<?php if (is_home() && !is_paged() ) {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args);
} ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="post<?php echo ++$counter; ?>">
<?php if ( is_home() && !is_paged() && $counter == 1 ) { ?>
do stuff
<?php } else { ?>
do other stuff
<?php } ?>
</div>
<?php endwhile; ?>