php - Wordpress category.php 返回空
问题描述
因此,我正在尝试创建一个列出特定类别的所有文章的 Wordpress 类别登录页面 (category.php)。出于某种原因,我的 category.php 循环返回空(即使文章确实存在于我单击的类别中)。我已经查看了大量关于如何创建此页面的教程,并且我的代码都与它们匹配,所以不确定为什么会发生这种情况。
旁注,我的 Wordpress 设置方式是每个自定义帖子类型下都有类别。
有什么帮助吗?谢谢!
<?php get_header(); ?>
<div class="container-fluid cat-landing">
<!--Logic for Category Listings-->
<div class="row">
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>
<h2 class="title"><?php single_cat_title(); ?> Category</h2>
<?php
// The Loop
while ( have_posts() ) : the_post();
?>
<div class="col-md-3 latest-post post-listing">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<a href="<?php the_permalink() ?>" class=""><img src="<?php echo $url ?>" /></a>
<div class="desc">
<a href="<?php the_permalink() ?>"><?php the_title();?></a>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
</div>
解决方案
以下内容也适用于类别,因为两者(分类法、类别、标签...)都使用相同的原则。我在这里发表了一篇关于此的广泛帖子,请不要犹豫看看。
在您的情况下,当您搜索时,
./fruits/apples
您会获得与术语 apples 相关的所有帖子,这些帖子要么显示在要么上,要么taxonomy.php
上taxonomy-fruits.php
或最后上taxonomy-fruits-aples.php
。但是如果你想访问./fruits/
呢?为什么访问水果会返回404.php
错误?好吧,因为taxonomy.php
旨在显示针对术语的帖子,而不是针对分类法的帖子。这是正常和预期的行为。
来源@为什么在没有任何条款的情况下尝试访问 taxonomy.php 页面时出现 404 错误,(Wordpress 看不到分类页面)
编辑 1.1: 我已经在本地完成了测试,你的代码似乎运行良好。
tht_title();
不需要回显,因为默认回显设置为true
https://developer.wordpress.org/reference/functions/the_title/
默认类别archive.php
用作主要后备,您应该检查是否有存档文件。您可以在这里查看模板层次结构https://developer.wordpress.org/themes/basics/template-hierarchy/。如果返回空白页面并且您仍然可以访问.fruit/apples
类别术语,那么您可能没有在该类别中注册帖子。
基本循环:
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo the_title().'<br/>';
endwhile; else:
echo 'No posts were found!';
endif; ?>