最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Notice: Trying to get property of non-object in : custom fields category

programmeradmin0浏览0评论

I have custom fields in my category. I'm using this tutorial to add custom fields to my categories.

I'm trying to output one of the custom fields on my category.php template but I'm unable to do so. The code I added to my template is:

<?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;
?>

I have turned on debugging on and it shows error:

Notice: Trying to get property of non-object in /var/www/animedownloads.nu/public_html/wp-content/themes/twentytwelve/category.php on line 26

Here's the template code:

<?php
/**
 * The template for displaying Category pages
 *
 * Used to display archive-type pages for posts in a category.
 *
 * @link 
 *
 * @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(); ?>

Anyone know how to fix this?

I have custom fields in my category. I'm using this tutorial to add custom fields to my categories.

I'm trying to output one of the custom fields on my category.php template but I'm unable to do so. The code I added to my template is:

<?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;
?>

I have turned on debugging on and it shows error:

Notice: Trying to get property of non-object in /var/www/animedownloads.nu/public_html/wp-content/themes/twentytwelve/category.php on line 26

Here's the template code:

<?php
/**
 * The template for displaying Category pages
 *
 * Used to display archive-type pages for posts in a category.
 *
 * @link http://codex.wordpress/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(); ?>

Anyone know how to fix this?

Share Improve this question edited Apr 12, 2014 at 5:13 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Apr 12, 2014 at 1:33 RurikoRuriko 5433 gold badges19 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Most probably the term was not found and $current_term returned as false:

$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$term_id = false === $current_term->term_id ? null : $current_term->term_id;

According to the docs, get_term_by() return values are

Term Row (object or array) from database. Will return false if $taxonomy does not exist or $term was not found. Othewise returns object (by default) or array of fields depending on $output parameter.

Output the $current_term value and see if it is false:

$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
var_dump( $current_term );
$term_id = $current_term->term_id;
发布评论

评论列表(0)

  1. 暂无评论