【发布时间】:2015-08-01 20:41:34
【问题描述】:
我正在构建一个 WP 网站,其中包含新闻和博客。
它们将位于不同的页面中,一个用于新闻,一个用于博客,它们将按类别分开。
例如,我在“新闻”上有这段代码,它会阻止循环获取帖子:
<?php
$uncat = get_cat_ID('uncategorised');
$uncat2 = get_cat_ID('blog');
$args = array(
'posts_per_page' => 3,
'category__not_in' => array($uncat, $uncat2)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h4><?php the_title(); ?></h3>
<span class="entry-date-blue"><strong><?php echo get_the_date('d/m/y'); ?></strong></span>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> <a href="">Read More ...</a>' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
</div>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
这很好用,但我在右侧也有“档案”,它按发布日期过滤。问题是,这会从新闻和博客中获取帖子,这破坏了将它们分开的想法。
有没有办法将它们分开,所以如果用户在存档中点击“2015 年 3 月”,它只会从 NEWS 获取本月的帖子?
这是我当前的 Archive.php 代码
<?php if (have_posts()) : ?>
<!-- First, the loop checks whether any posts were discovered with the have_posts() function. -->
<!-- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating). -->
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-12 col-sm-12 col-xs-12">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<h5 class="captext"><?php the_title(); ?></h5>
<span class="entry-date-orange"><?php echo get_the_date(); ?></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
【问题讨论】:
-
有点不清楚你在问什么,
news和blog是什么。它们是术语、类别、自定义帖子类型……吗?另外,如果我理解正确的话,这实际上是一个非常大的问题需要回答,因为您很可能会更好地编写自己的小部件并在 URL 中添加参数以充当引用者。恕我直言,当我读到这篇文章时,news和blog应该是自定义帖子类型 -
嗨 Pieter :) 新闻和博客只是浏览最近的帖子。存档从某个日期以及热门标签中提取最近的帖子
-
好的,但同样,
news和blog是什么。重读我的第一条评论 -
抱歉,它们只是 .php 文件,被用作 WordPress 页面的页面模板
-
所以,你使用新闻页面来显示究竟是什么,而博客页面来显示究竟是什么