我已经阅读了WordPress上的多循环规范,但我仍然对如何做到这一点感到有点困惑。
我必须从默认查询中运行3个查询。
-1 =来自X类别的最新帖子
-2 =所有来自Y类别的帖子
-3 =所有帖子,除了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_queries,但是第二个不能加载到我的page上。正因为如此,我没有尝试过第三个。首先,wp_query是否适用于默认循环?其次,我如何重写这个代码,这样它就可以处理更多的循环了。
发布于 2016-07-21 00:31:16
你的代码有几个问题,但我不认为循环是不正确的。我可以在您的页面源代码中看到在第二个循环中生成的内容;看起来Owl Carousel没有被初始化。
问题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;?>
...可能还有其他问题,但这两个应该给你一个开始,并让你(和我们)看到任何其他可能是错误的东西。
https://stackoverflow.com/questions/38471723
复制相似问题