我的类别中有自定义字段。我正在使用this tutorial 将自定义字段添加到我的类别。
我正在尝试输出类别中的一个自定义字段。php模板,但我无法这样做。我添加到模板中的代码是:
<?php
$term_slug = get_query_var( \'term\' );
$taxonomyName = get_query_var( \'taxonomy\' );
$current_term = get_term_by( \'slug\', $term_slug, $taxonomyName );
$term_id = $current_term->term_id;
$saved_data = get_tax_meta($term_id,\'text_field_id\');
echo $saved_data;
?>
我已打开调试,它显示错误:
注意:尝试在/var/www/animedownloads中获取非对象的属性。nu/public\\U html/wp内容/主题/二十一世纪/类别。php第26行
以下是模板代码:
<?php
/**
* The template for displaying Category pages
*
* Used to display archive-type pages for posts in a category.
*
* @link http://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/
get_header(); ?>
<section id="primary" class="site-content">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<header class="archive-header">
<h1 class="archive-title"><?php printf( __( \'Category Archives: %s\', \'twentytwelve\' ), \'<span>\' . single_cat_title( \'\', false ) . \'</span>\' ); ?></h1>
<?php
$term_slug = get_query_var( \'term\' );
$taxonomyName = get_query_var( \'taxonomy\' );
$current_term = get_term_by( \'slug\', $term_slug, $taxonomyName );
$term_id = $current_term->term_id;
$saved_data = get_tax_meta($term_id,\'text_field_id\');
echo $saved_data;
?>
<?php if ( category_description() ) : // Show an optional category description ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
</header><!-- .archive-header -->
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
/* Include the post format-specific template for the content. If you want to
* this in a child theme then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( \'content\', get_post_format() );
endwhile;
twentytwelve_content_nav( \'nav-below\' );
?>
<?php else : ?>
<?php get_template_part( \'content\', \'none\' ); ?>
<?php endif; ?>
</div><!-- #content -->
</section><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
有人知道怎么解决这个问题吗?
最合适的回答,由SO网友:RRikesh 整理而成
很可能没有找到术语,并且$current_term
返回为false
:
$current_term = get_term_by( \'slug\', $term_slug, $taxonomyName );
$term_id = false === $current_term->term_id ? null : $current_term->term_id;
根据文件,
get_term_by()
返回值为
数据库中的术语行(对象或数组)。如果$taxonomy不存在或未找到$term,将返回false。Othewise根据$输出参数返回对象(默认)或字段数组。
输出$current_term
值并查看是否为false
:
$current_term = get_term_by( \'slug\', $term_slug, $taxonomyName );
var_dump( $current_term );
$term_id = $current_term->term_id;