Wordpress 在每月存档中按类别对帖子进行分组
我试图在每月的档案中按类别对帖子进行分组,但不知道如何做到这一点 这里有人能帮我吗 问候Wordpress 在每月存档中按类别对帖子进行分组,wordpress,archive,Wordpress,Archive,我试图在每月的档案中按类别对帖子进行分组,但不知道如何做到这一点 这里有人能帮我吗 问候 这是我在archive.php中使用的php代码 <?php $terms = get_terms("publicationcat"); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { echo
这是我在archive.php中使用的php代码
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
唯一的问题是,它会为所有术语显示相同的帖子标题。当你在每个类别中循环浏览帖子时,你不会只找到与该类别相关的帖子,而是在每个帖子中循环浏览。下面的内容(未经测试,因此如果出现错误,请进行评论)应该会对您的问题进行排序 但是请注意,如果一篇文章被分配到多个类别,你可能仍然会得到一些重复 检查每个帖子,确保其属于所需类别。好的观点-只有一个查询。坏点-循环通过每个猫的所有帖子
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
嗨,Himanshu,你现在使用什么函数来输出归档文件?嗨,Barry,我刚刚编辑了问题并粘贴了php代码..你好,David,我刚刚尝试了你的代码,但它不起作用,也没有显示任何内容。此外,我正在使用显示日期档案的代码,因此是否适合运行自定义查询?请立即尝试。我已经纠正了一个打字错误并测试了这个,在我的网站上运行良好。这段代码也适用于您的存档,您只需根据您的日期范围修改查询即可。好的,因此我添加了另一种可能更适合存档页面的方法(两个示例中的第一个)。总的来说,它可能效率较低,但在处理日期时,它更容易、更快,因为您不必手动构造查询。您能重新发布一个我可以在存档页面中使用的查询吗?
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>