Wordpress-设置每个类别的自定义帖子
我已经在functions.php文件中添加了以下代码,以便为每个类别加载不同的帖子模板。代码正在做它应该做的事情。我面临的问题是,当我正确地打开POST时,加载过程中会立即显示一个错误:“试图在……functions.php中获取非对象的属性”,我如何才能消除该错误?谢谢你的帮助Wordpress-设置每个类别的自定义帖子,wordpress,Wordpress,我已经在functions.php文件中添加了以下代码,以便为每个类别加载不同的帖子模板。代码正在做它应该做的事情。我面临的问题是,当我正确地打开POST时,加载过程中会立即显示一个错误:“试图在……functions.php中获取非对象的属性”,我如何才能消除该错误?谢谢你的帮助 /* ---------------------------------------------------------------------------- custom single posts pe
/* ----------------------------------------------------------------------------
custom single posts per category
*/
add_filter('single_template', 'check_for_category_single_template');
function check_for_category_single_template( $t )
{
foreach( (array) get_the_category() as $cat )
{
if ( file_exists(get_stylesheet_directory() . "/single-category-{$cat->slug}.php") ) return get_stylesheet_directory() . "/single-category-{$cat->slug}.php";
if($cat->parent)
{
$cat = get_the_category_by_ID( $cat->parent );
if ( file_exists(get_stylesheet_directory() . "/single-category-{$cat->slug}.php") ) return get_stylesheet_directory() . "/single-category-{$cat->slug}.php";
}
}
return $t;
}尝试获取非对象的属性时出现的错误
意味着,您尝试获取非对象的某个属性。因此,看看你的代码,你会得到两个属性:slug和parent
这给了我们一个提示,$cat
变量中保存的值可能不是对象
您正在使用get\u the \u category\u by \u ID
,它为您提供了一个名称,而不是一个category对象。因此,您无法访问任何属性。问题是:通过\u ID($cat->parent)获取\u类别\u代码>在$cat中没有父属性,因为它只包含名称。您需要获取对象以使用父属性。此外,该属性被称为category\u parent
,而不是parent
$parent_id = $cat->category_parent;
$cat = get_category($parent_id);
这应该能解决你的问题
如果它不能解决您的问题,我将用另一种方式编写代码。它显示了单个步骤,可能对您或其他用户有用
获取所有类别对象的函数是:get\u categories()
您可以将categoires的所有ID保存到一个数组中。然后,循环遍历数组的每个位置,检查正在查看的单个帖子是否具有相同的类别ID。我们可以使用in_category()
我没有进行测试,但代码可能看起来像:
add_filter('single_template', 'check_for_category_single_template');
function check_for_category_single_template() {
// Get all category objects
$categories = get_categories();
// Array for IDs
$categories_ids = [];
foreach($categories as $category) {
// add id to array
array_push($categories_ids, $category->cat_ID);
}
// Loop through array of IDs
foreach($categories_ids as $category_id) {
// Check if ID equals current post category ID
if ( in_category($category_id) ) {
// get current category object
$cat = get_category($category_id);
// check if category has parent
if( $cat->category_parent > 0 ) {
$parent_id = $cat->category_parent;
// get parent object
$cat = get_category( $parent_id );
}
// get slug of category
$cat_slug = $cat->slug;
if ( file_exists(get_stylesheet_directory() . "/single-category-" .$cat_slug. ".php") ) {
return get_stylesheet_directory() . "/single-category-" .$cat_slug. ".php";
}
}
}
}
该错误消息还将包括导致错误的行号。否则,我们将无法帮助您。感谢您的反馈。它指的是第28行,即:如果(文件_存在(get_样式表_目录()。“/single category-{$cat->slug}.php”))返回get_样式表_目录()。“/singlecategory-{$cat->slug}.php”;同样重要的是要注意,只有在我打开子类别中的页面时,才会出现错误(加载时出现一瞬间)。类别中的页面不会触发错误…请提供任何帮助:)谢谢您的回答,建议的更改仍会触发错误。我还测试了您提供的代码,除了打开子类别中的帖子外,它工作正常。出现以下错误:注意:在第51行的/home4/../functions.php中尝试获取非对象的属性注意:未定义的属性:WP\u错误::$slug在第55行的/home4/../functions.php第51行是:$cat=get\u category($parent\u id->category\u parent);第55行是:$cat_slug=$cat->slug;我可以怎么修复它呢?哦,我有打字错误$cat=get\u category($parent\u id->category\u parent)代码>是错误的。Get\u category只需要ID。因此我将该行更正为Get\u category($parent\u ID)
,或许可以再试一次