我需要为我的博客home.php创建自定义多个循环,以便显示已定义类别的3个不同内容布局,然后继续使用在前3个循环中排除帖子ID的常规循环。
到目前为止,我制作了定义类别category_name=arts
的3种不同内容布局:
<?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php $count2++; } ?>
<?php endforeach; ?>
<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "1") { break; } else { ?>
<?php the_title(); ?>
<?php $count3++; } ?>
<?php endforeach; ?>
我确实陷入了继续循环。任何建议都非常感谢。
答案 0 :(得分:0)
你这里有严重的问题
对于每个循环,您运行两个查询,一个使用get_posts()
,另一个使用query_posts
您正在使用永远不会使用的query_posts
。当您重新运行主查询时,这会增加查询的巨大开销,因此您实际上运行3个查询以获得每个循环的1个帖子。这会降低你的页面速度,这会让你在SEO方面付出沉重的代价
此外,query_posts
打破了主查询对象,该对象将数千个函数分解为依赖于主查询对象的插件。你应该花时间阅读this post,了解真正的query_posts
是多么糟糕。
start_wp()
已在WordPress 1.5版中弃用。这意味着您的代码存在错误,应该不惜一切代价避免错误。您应该在开发时打开调试,因为调试将立即抛出一条调试消息,告诉您正在使用折旧函数。 Here is an article from the codex关于如何使用WordPress中的调试功能。您应该使用setup_postdata( $post )
仍在调试部分,showposts
被弃用,转而使用posts_per_page
您正在运行foreach
循环而不确定是否有要显示的帖子。在尝试对动态变量执行任何操作之前,始终始终确保您具有有效值。如果您的变量返回空值,这将避免许多错误。
您应该真正处理格式化,因为当所有内容都打包成一行时,您的代码很难阅读。使用适当的缩进。正确缩进和格式化的代码在可读性和调试方面确实有很大帮助。另外,删除:
和endforeach
语法。虽然它是有效的,但代码编辑器不支持它,如果代码失败,它会使调试成为一场噩梦。我总是告诉大家使用旧式大括号,因为所有代码编辑都支持它们。它们使调试变得非常容易
始终非常重要,始终重置自定义查询。在自定义查询中调用wp_reset_postdata()
或$post
时,使用setup_postdata()
重置the_post()
全局。仅仅是为了感兴趣,因为我已经声明永远不会使用query_posts
,您应该使用wp_reset_postdata()
来重置使用query_posts
最后,您只需要在一个循环中执行指定类别所需的操作,而不是三个循环。只需使用您的计数器。如果这纯粹是为了造型,您只需使用:nth child()
selector in css3
以下是未经测试的,但您可以尝试以下
$special_cat_args = [
'category_name' => 'art',
'posts_per_page' => 3,
//Add extra arguments here
];
$art_posts = get_posts( $special_cat_args );
// Setup a variable to store the post ID's so we can exclude them in the 'main' query
$ids_array = [];
// Check if we have posts before we continue
if ( $art_posts ) {
// Start our counter
$counter = 0;
// Start the loop
foreach ( $art_posts as $post ) {
// Setup postdata, must use $post
setup_postdata( $post );
// Store the post id in an array to exclude in "main" query
$ids_array[] = $post->ID;
// Do something separate for every post
if ( 0 == $counter ) {
// Do something for post one
} elseif ( 1 == $counter ) {
// Do something for post two
} elseif ( 2 == $counter ) {
// Do something for post three
}
// Update the counter
$counter++;
} //endforeach $art_posts
wp_reset_postdata(); // VERY VERY IMPORTANT
} //endif $art_posts
// Now we can do our "main" query and exclude the three posts from the special category
$args = [
'post__not_in' => $ids_array, // Exclude the three post from previous query
// Rest of your arguments
];
$q = get_posts( $args );
// Run your loop