我已经阅读了WordPress上的多循环编码,但我仍然有点困惑如何做到这一点。
我必须从默认查询运行3个查询。
-1=来自X类别的最新帖子
-2=来自Y类别的所有帖子
-3=除了最近的(因为它被拉入-1)X类别之外的所有帖子
这就是我目前设置index.php的方式:
<?php
get_header(); ?>
<?php
$args = array(
'posts_per_page' => 1,
'category_name' => 'news',
);
$news_query = new WP_Query( $args );
if ( $news_query->have_posts() ) :
?>
<?php while ( $news_query->have_posts() ) : $news_query->the_post(); ?>
<?php
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$date = Date('m/y');
$title = get_the_title($post->ID);
$link = get_the_permalink($post->ID);
?>
<?php echo '<div class="recent-news-banner" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; ?>
<?php echo '<div class="container"><div class="row"><div class="col-md-8"><div class="' . 'entry-meta' . '"><div class="meta-date">' . $date . '</div><div class="meta-info"><div class="meta-title"><h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2><div class="meta-excerpt">' . get_the_excerpt() . '</div></div></div></div></div></div>'; ?>
<?php echo '</div>'; ?>
<?php endwhile;?>
<?php else : ?>
<p><?php _e( "No News Found", 'news' ); ?></p>
<?php wp_reset_postdata(); ?>
<?php endif
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$args = array(
'posts_per_page' => -1,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
echo '<div id="owl-testimonials" class="owl-theme owl-carousel container"><div class="row"><div class="col-xs-12">';
if ( $events_query->have_posts() ) :
?>
<?php while ( $events_query->have_posts() ) : $events_query->the_post(); ?>
<?php
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$date = Date('F j, Y');
$title = get_the_title($post->ID);
$link = get_the_permalink($post->ID);
?>
<?php echo '<div class="event-item" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; ?>
<?php echo '<div class="meta-date">' . $date . '</div>'; ?>
<?php echo '</div>'; ?>
<?php echo '<div class="meta-info"><div class="meta-title"><h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3></div></div>'; ?>
<?php endwhile;?>
<?php echo '</div></div></div>'; ?>
<?php else : ?>
<p><?php _e( "No News Found", 'news' ); ?></p>
<?php wp_reset_postdata(); ?>
<?php endif
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
我把它写成了两个wp_query,但第二个不会加载到我的页面上。正因为如此,我还没有尝试过第三次。首先,wp_query是否适用于默认循环?第二,我如何重新处理它,使它能处理更多的循环。
您的代码有几个问题,但我不认为循环是错误的。我能够在您的页面源中看到第二个循环中生成的内容;看来猫头鹰旋转木马没有被初始化。
问题1:没有向$
分配任何内容,因此$('#owl-events').owlCarousel(...)
调用不起作用(事实上,它看起来main.js
中的任何内容都不起作用。您需要让jQuery接管$
,或者显式使用jQuery
而不是$
。
第2期:未关闭divs
。在第一个循环中,您回显9个div,但只关闭其中的8个。当把它们都堆在一行时,这真的很容易错过。你不需要echo
这些内容,你只需要在页面上内联它们,并在需要的地方穿插php,并维护一些格式,这样你就可以看到发生了什么。例如:
...
<?php while ( $news_query->have_posts() ) : $news_query->the_post(); ?>
...
<div class="recent-news-banner" style="background: url(<?= $featured_img ?>') no-repeat center center; background-size: cover;">'; ?>
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="entry-meta">
<div class="meta-date"><?= $date ?></div>
<div class="meta-info">
<div class="meta-title">
<h2>
<a href="<?= get_permalink() ?>"><?= get_the_title() ?></a>
</h2>
<div class="meta-excerpt"><?= get_the_excerpt() ?></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- uh oh, not enough closing divs -->
<?php endwhile;?>
...
可能还有其他问题,但这两个问题应该给你一个开始,让你(和我们)看到任何其他可能出错的事情。