WordPress 多循环

发布于 2024-12-10 17:34:44 字数 577 浏览 0 评论 0原文

我在尝试在 WordPress 中构建多个循环时感到非常沮丧。我看了很多文章 - 我做错了什么。我将以下内容放入了loop.php 文件中(因为我已经在此基础上构建了主页)...

<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...

<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

涙—继续流 2024-12-17 17:34:44
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

  // your output html   

<?php endwhile; ?>

您已经嵌套了循环(缺少 endwhile)。

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

  // your output html   

<?php endwhile; ?>

You have nested the loops (missing endwhile).

把梦留给海 2024-12-17 17:34:44

您不能只是发明一个变量“query_string2”并期望它起作用;)

试试这个:

<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php 
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

如果这不起作用(我不记得 wp_reset_query() 是否重置了 $query_string< /code> global 超出了我的想象),请尝试以下操作:

<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php 
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

You cannot just invent a variable "query_string2" and expect it to work ;)

Try this:

<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php 
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

If that doesn't work (I don't remember whether wp_reset_query() resets the $query_string global off the top of my head), try the following:

<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>

<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php 
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>

<?php while ( have_posts() ) : the_post(); ?>
埖埖迣鎅 2024-12-17 17:34:44

这会起作用:

<?php
    global $query_string;        //make sure these are in the correct format for post queries
    global $query_string1;

    // First loop
    $first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');

    while ( $first_loop->have_posts() ) : $first_loop->the_post() :
        // do your thing
        // e.g: $first_loop->the_content();
    endwhile;


    // second loop
    $second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');

    while ( $second_loop->have_posts() ) : $second_loop->the_post() :
        //do your thing
    endwhile;

    // Reset Post Data 
    wp_reset_postdata();

?>

This will work:

<?php
    global $query_string;        //make sure these are in the correct format for post queries
    global $query_string1;

    // First loop
    $first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');

    while ( $first_loop->have_posts() ) : $first_loop->the_post() :
        // do your thing
        // e.g: $first_loop->the_content();
    endwhile;


    // second loop
    $second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');

    while ( $second_loop->have_posts() ) : $second_loop->the_post() :
        //do your thing
    endwhile;

    // Reset Post Data 
    wp_reset_postdata();

?>
假装爱人 2024-12-17 17:34:44

我可以帮忙,但我认为首先,我应该了解一个基本原则:你到底想做什么?你的目标是什么?为什么你认为你需要嵌套循环?

这将帮助我更正确地提问。

I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?

This will help me aswering more correctly.

唐婉 2024-12-17 17:34:44

我用下面的代码解决了这个问题,我将其放入页面模板中(我的朋友建议最好保留loop.php)

<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>

<?php wp_reset_postdata(); // reset the query ?>

<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>

I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)

<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>

<?php wp_reset_postdata(); // reset the query ?>

<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
鼻尖触碰 2024-12-17 17:34:44

有一个名为 rewind_posts() 的函数,它可以倒回循环帖子,以便您可以在同一页面的不同位置重复使用相同的查询

https://codex.wordpress.org/Function_Reference/rewind_posts

There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page

https://codex.wordpress.org/Function_Reference/rewind_posts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文