[英]Wordpress category.php returning empty
因此,我正在尝试创建一个 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();
不需要回显,因为默认情况下 echo 设置为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; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.