Wordpress - 包含具有类别名称的 php 文件?

Wordpress - Include php file with name of category?

我正在尝试包含一个 php 文件,其中包含我的每个类别的描述。所以当我在类别 Cat1 时,我想查看 Cat1 的描述,当我在 Cat2 时,我想查看 Cat2 的描述等等。

我尝试了以下代码:

<?php 
    include (single_cat_title() . '.php');
?>

但它只显示类别的标题。当我尝试这个时:

<?php
    $name = "Cat1";
    include ($name . '.php');
?>

它有效,但是如果我添加 single_cat_title() 而不是硬编码 "Cat1" 它停止工作。

谢谢!

试试这个:

<?php include (single_cat_title('',false) . '.php'); ?>

引用自:https://developer.wordpress.org/reference/functions/single_cat_title/#parameters

你可以试试

<?php 
    include (single_cat_title("", false) . '.php');
?>

<?php 
    include (get_query_var('category_name') . '.php');
?>