Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.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(); ?>

Anyone know how to fix this?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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

$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$term_id = $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;
share|improve this answer
    
Yes it returned the value false so how can i fix this? –  Ruriko Apr 13 at 2:42
    
Returning false means that the taxonomy does not exist or the term was not found. Double check if the term exists and the names that you are using are right. –  RRikesh Apr 13 at 4:21
    
The term should exist since I'm viewing the term page. Here is live version animedownloads.nu/category/mai-hime I really have no idea what code is wrong –  Ruriko Apr 13 at 4:43
    
I just noticed that you're calling get_query_var outside the loop, can you try to move it just after the line with while ( have_posts() ) : the_post(); ? –  RRikesh Apr 13 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.