wordpress - 如何访问 WordPress 中的 category.php 页面?
问题描述
我已经category.php
在我的主题中创建并添加了一些代码。下面的代码将显示我的类别列表。现在我想知道如何访问此category.php
页面?
当我访问时,mywebsite.com/category
我得到Page not found
.
我在 category.php 页面中添加了以下代码
<?php
/**
* A Simple Category Template
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div class="CategoryHero">
<?php
// Get the current queried object
$term = get_queried_object();
$term_id = ( isset( $term->term_id ) ) ? (int) $term->term_id : 0;
$categories = get_categories( array(
'taxonomy' => 'category',
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
?>
<div class="categoryWrapper_list"><div class="equalPadding">
<ul>
<?php
foreach ( $categories as $category )
{
//echo "<pre>";
//var_dump($category);
$cat_ID = $category->term_id;
$category_name = $category->name;
$category_desc = $category->description;
$category_images = get_option('category_images');
// When viewing a particular category, give it an [active] class
$cat_class = ( $cat_ID == $term_id ) ? 'active' : 'not-active';
// I don't like showing the [uncategoirzed] category
if ( strtolower( $category_name ) != 'uncategorized' ){?>
<li> <div class="subCategoryPostWrap" style="background-image:linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)),url(<?php echo $category_images[$cat_ID];?>)">
<div class="blackBoxPackage">
<div class="cp-b-content">
<h4><?php echo $category_name;?></h4>
<p><?php echo $category_desc;?></p>
<div class="canExplore">
<a class="canbtn" href="<?php echo esc_url(get_category_link($cat_ID));?>">Explore now</a>
</div>
</div>
</div>
</div>
</li>
<?php
}
}
?>
</ul>
</div>
</div>
</div>
</main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
解决方案
我不完全确定这会解决你的问题,但你需要让 WordPress 知道这是一个自定义模板,方法是在文件顶部写一个打开的 PHP 注释来说明模板的名称。
<?php
/* Template Name: A Simple Category Template */
?>
推荐阅读
- reactjs - 获取 401 授权问题
- excel - 在 Excel 加载项中重新加载自定义函数 - 在线版
- python - Python - 从子模块中的类继承
- javascript - 学习在 wix 的 velo 上创建基本的颜色分配工具
- python - 在 discord.py 中,如何制作脏话过滤器
- python - 文字标签不显示
- imagej - 如何在运行时查找、读取、写入 Jython 注册表
- eclipse - java.lang.NullPointerException:条目中的空值:lastScrollData=null
- sql - 在 SQL 中聚合具有条件的行
- angular - @agm/core 模块在 '@angular/core' 中找不到 angular 7 'ɵɵdefineInjectable' 错误