Ask your WordPress questions! Pay money and get answers fast! (more info)

How to display custom fields for my custom taxonomy WordPress

  • SOLVED

Hi, I'm trying to display the <em>custom fields</em> for my <em>custom taxonomy</em> named <em>"Authors"</em>.
The <em>custom fields</em> are filled with author bio information that I need displayed on my <em>index template</em> above the author quotes when a respective author taxonomy is viewed.

This is the tutorial I used to setup the custom fields. http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies

The custom fields seem to work properly i.e. they display correctly on the author edit page and they save.

Here is where the problem lies.
I add this to my <em>index template</em> to associate everything.

<?php // Get the custom fields based on the $author term ID
$author_custom_fields = get_option( "taxonomy_term_$author->term_id" );

// Return the value for the "author" custom field
$author_data = get_userdata( $author_custom_fields[name] ); // Get their data ?>


And this also to my <em>index template</em> to display the <em>taxonomy custom field data</em>.

<?php echo $author_data->user_description;
$author_data->photo;
$author_data->name;
$author_data->aka;
$author_data->birth;
$author_data->birth_place;
$author_data->death; ?>


And nothing referencing the code above displays on my index template.

I am hoping with someones help we can fix this, as it's my last major hurdle before launch.
Thank you.

Answers (1)

2013-11-29

Fahad Murtaza answers:

Hey Shane

Can you share the code which creates the custom field and taxonomies itself. Will make it easy for me to test.


Shane Pennington comments:

Fahd, thanks for taking a look at this.

// custom author taxonomy
add_action( 'init', 'register_taxonomy_authors' );

function register_taxonomy_authors() {

$labels = array(
'name' => _x( 'Authors', 'authors' ),
'singular_name' => _x( 'Author', 'authors' ),
'search_items' => _x( 'Search Authors', 'authors' ),
'popular_items' => _x( 'Popular Authors', 'authors' ),
'all_items' => _x( 'All Authors', 'authors' ),
'parent_item' => _x( 'Parent Author', 'authors' ),
'parent_item_colon' => _x( 'Parent Author:', 'authors' ),
'edit_item' => _x( 'Edit Author', 'authors' ),
'update_item' => _x( 'Update Author', 'authors' ),
'add_new_item' => _x( 'Add New Author', 'authors' ),
'new_item_name' => _x( 'New Author', 'authors' ),
'separate_items_with_commas' => _x( 'Separate authors with commas', 'authors' ),
'add_or_remove_items' => _x( 'Add or remove Authors', 'authors' ),
'choose_from_most_used' => _x( 'Choose from most used Authors', 'authors' ),
'menu_name' => _x( 'Authors', 'authors' ),
);

$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => false,
'show_admin_column' => false,
'hierarchical' => false,

'rewrite' => true,
'query_var' => true
);

register_taxonomy( 'authors', array('post'), $args );
}



// A callback function to add a custom field to our "authors" taxonomy
function authors_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>

<tr class="form-field">
<th scope="row" valign="top">
<label for="photo"><?php _e('Photo'); ?></label>
</th>
<td>
<input type="text" name="term_meta[photo]" id="term_meta[photo]" size="25" style="width:60%;" value="<?php echo $term_meta['photo'] ? $term_meta['photo'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Photo URL'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="name"><?php _e('Name'); ?></label>
</th>
<td>
<input type="text" name="term_meta[name]" id="term_meta[name]" size="25" style="width:60%;" value="<?php echo $term_meta['name'] ? $term_meta['name'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Birth Name'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="aka"><?php _e('AKA'); ?></label>
</th>
<td>
<input type="text" name="term_meta[aka]" id="term_meta[aka]" size="25" style="width:60%;" value="<?php echo $term_meta['aka'] ? $term_meta['aka'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Nickname'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="birth"><?php _e('Birth'); ?></label>
</th>
<td>
<input type="text" name="term_meta[birth]" id="term_meta[birth]" size="25" style="width:60%;" value="<?php echo $term_meta['birth'] ? $term_meta['birth'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Birth Date'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="birth_place"><?php _e('Birth Place'); ?></label>
</th>
<td>
<input type="text" name="term_meta[birth_place]" id="term_meta[birth_place]" size="25" style="width:60%;" value="<?php echo $term_meta['birth_place'] ? $term_meta['birth_place'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Birth Place'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="death"><?php _e('Death'); ?></label>
</th>
<td>
<input type="text" name="term_meta[death]" id="term_meta[death]" size="25" style="width:60%;" value="<?php echo $term_meta['death'] ? $term_meta['death'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Death Date'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="star_sign"><?php _e('Star Sign'); ?></label>
</th>
<td>
<input type="text" name="term_meta[star_sign]" id="term_meta[star_sign]" size="25" style="width:60%;" value="<?php echo $term_meta['star_sign'] ? $term_meta['star_sign'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Zodiac'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="hit_movies"><?php _e('Hit Movies'); ?></label>
</th>
<td>
<input type="text" name="term_meta[hit_movies]" id="term_meta[hit_movies]" size="25" style="width:60%;" value="<?php echo $term_meta['hit_movies'] ? $term_meta['hit_movies'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Hit Movies'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="hit_songs"><?php _e('Hit Songs'); ?></label>
</th>
<td>
<input type="text" name="term_meta[hit_songs]" id="term_meta[hit_songs]" size="25" style="width:60%;" value="<?php echo $term_meta['hit_songs'] ? $term_meta['hit_songs'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Hit Songs'); ?></span>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top">
<label for="bio"><?php _e('Bio'); ?></label>
</th>
<td>
<input type="text" name="term_meta[bio]" id="term_meta[bio]" size="25" style="width:100%; height:200px;" value="<?php echo $term_meta['bio'] ? $term_meta['bio'] : ''; ?>"><br />
<span class="description"><?php _e('The Author\'s Bio Description'); ?></span>
</td>
</tr>

<?php
}



// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ){
if ( isset( $_POST['term_meta'][$key] ) ){
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
//save the option array
update_option( "taxonomy_term_$t_id", $term_meta );
}
}

// Add the fields to the "authors" taxonomy, using our callback function
add_action( 'authors_edit_form_fields', 'authors_taxonomy_custom_fields', 10, 2 );

// Save the changes made on the "authors" taxonomy, using our callback function
add_action( 'edited_authors', 'save_taxonomy_custom_fields', 10, 2 );








Fahad Murtaza comments:

/ Get the custom fields based on the $presenter term ID
$author_custom_fields = get_option( "taxonomy_term_$presenter->term_id" );

// Return the value for the "photo" custom field
$author_photo = get_userdata( $presenter_custom_fields[photo] );

// Return the value for the "name" custom field
$name = get_userdata( $presenter_custom_fields[name] );

// Return the value for the "aka" custom field
$aka = get_userdata( $presenter_custom_fields[aka] );


// Return the value for the "birth" custom field
$birth = get_userdata( $presenter_custom_fields[birth] );

// Return the value for the "birth_place" custom field
$birth_place = get_userdata( $presenter_custom_fields[birth_place] );

// Return the value for the "death" custom field
$birth_place = get_userdata( $presenter_custom_fields[death] );



Then use these variables above.


Fahad Murtaza comments:

/ Get the custom fields based on the $presenter term ID
$author_custom_fields = get_option( "taxonomy_term_$presenter->term_id" );

// Return the value for the "photo" custom field
$author_photo = get_userdata( $presenter_custom_fields[photo] );

// Return the value for the "name" custom field
$name = get_userdata( $presenter_custom_fields[name] );

// Return the value for the "aka" custom field
$aka = get_userdata( $presenter_custom_fields[aka] );


// Return the value for the "birth" custom field
$birth = get_userdata( $presenter_custom_fields[birth] );

// Return the value for the "birth_place" custom field
$birth_place = get_userdata( $presenter_custom_fields[birth_place] );

// Return the value for the "death" custom field
$birth_place = get_userdata( $presenter_custom_fields[death] );



Then use these variables above.


Fahad Murtaza comments:


<?php

$author_custom_fields = get_option( "taxonomy_term_$author->term_id" );

echo '<pre>';
print_r($author_custom_fields );
echo '</pre>';
?>


test above code and see what it shows as output if last solution fails. It should show the array containing all user info. Please share your output.


Shane Pennington comments:

I replaced "presenter" with "author" so do you mean:

/ Get the custom fields based on the $presenter term ID

$author_custom_fields = get_option( "taxonomy_term_$author->term_id" );



// Return the value for the "photo" custom field

$author_photo = get_userdata( $author_custom_fields[photo] );



// Return the value for the "name" custom field

$name = get_userdata( $author_custom_fields[name] );



// Return the value for the "aka" custom field

$aka = get_userdata( $author_custom_fields[aka] );





// Return the value for the "birth" custom field

$birth = get_userdata( $author_custom_fields[birth] );



// Return the value for the "birth_place" custom field

$birth_place = get_userdata( $author_custom_fields[birth_place] );



// Return the value for the "death" custom field

$birth_place = get_userdata( $author_custom_fields[death] );


Fahad Murtaza comments:

exactly, my bad!


Fahad Murtaza comments:

A much cleaner copy of my code


<?php

// Get the custom fields based on the $author term ID

$author_custom_fields = get_option( "taxonomy_term_$author->term_id" );


// Return the value for the "photo" custom field

$author_photo = get_userdata( $author_custom_fields[photo] );


// Return the value for the "name" custom field

$name = get_userdata( $author_custom_fields[name] );


// Return the value for the "aka" custom field

$aka = get_userdata( $author_custom_fields[aka] );


// Return the value for the "birth" custom field

$birth = get_userdata( $author_custom_fields[birth] );


// Return the value for the "birth_place" custom field

$birth_place = get_userdata( $author_custom_fields[birth_place] );


// Return the value for the "death" custom field

$death = get_userdata( $author_custom_fields[death] );


echo $author_photo;


echo $name;


echo $aka;


echo $birth;


echo $birth_place;


echo $death;

?>


Shane Pennington comments:

Unfortunately it had no effect. Im including my index.php code below just incase I missed something.
Its a mess I'm sure... still learning lol

<?php get_header(); ?>



<?php
if ( is_category() && 'on' === et_get_option( 'nexus_category_featured', 'on' ) )
get_template_part( 'includes/featured' );
?>

<div class="page-wrap container">
<div id="main-content">
<div class="main-content-wrap clearfix">
<div id="content">
<?php get_template_part( 'includes/breadcrumbs', 'index' ); ?>

<div id="left-area">




<?php



// Get the custom fields based on the $author term ID



$author_custom_fields = get_option( "taxonomy_term_$author->term_id" );





// Return the value for the "photo" custom field



$author_photo = get_userdata( $author_custom_fields[photo] );





// Return the value for the "name" custom field



$name = get_userdata( $author_custom_fields[name] );





// Return the value for the "aka" custom field



$aka = get_userdata( $author_custom_fields[aka] );





// Return the value for the "birth" custom field



$birth = get_userdata( $author_custom_fields[birth] );





// Return the value for the "birth_place" custom field



$birth_place = get_userdata( $author_custom_fields[birth_place] );





// Return the value for the "death" custom field



$death = get_userdata( $author_custom_fields[death] );





echo $author_photo;





echo $name;





echo $aka;





echo $birth;





echo $birth_place;





echo $death;



?>

<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$thumb = '';
$width = (int) apply_filters( 'et_index_image_width', 240 );
$height = (int) apply_filters( 'et_index_image_height', 240 );
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
$thumb = $thumbnail["thumb"];
?>
<span class="bqstartindex">&#8220;</span>
<div class="recent-post-index">

<?php if ( '' !== $thumb ) : ?>
<div class="et-main-image">
<a href="<?php the_permalink(); ?>">
<!--<?php print_thumbnail( $thumb, $thumbnail[""], $titletext, $width, $height ); ?>-->
</a>

<div class="meta-info">
<div class="meta-date">

</div>
</div>
</div>
<?php endif; ?>

<div class="et-description">


<?php if ( ( $review_rating = get_post_meta( get_the_ID(), '_et_author_rating', true ) ) && '' !== $review_rating ) : ?>
<span class="review-rating"><span style="width: <?php echo $review_rating * 33.5; ?>px;"></span></span>
<?php endif; ?>

<?php et_nexus_post_meta(); ?>
<span class="class1">
<a href="<?php the_permalink(); ?>">

<?php
the_content();
?>
</a>
</span>
<?php
echo '<div class="share-time-index">';
echo do_shortcode ('[shareaholic app="share_buttons" id="435909"]');
echo '</div>';
echo '<div class="term-list-index-author">';
echo '<span class="class2">';
echo get_the_term_list( $post->ID, 'authors', ' - ', ', ', ' ' );
echo get_the_term_list( $post->ID, 'movies', ' ,', ', ', ' ' );
echo '</span>';
echo '</div>';


?>


<style type="text/css">
.class1 A:link {
text-decoration: none;
color: #444444;
}
.class1 A:visited {
text-decoration: none;
color: #444444;
}
.class1 A:active {
text-decoration: none;
}
.class1 A:hover {
text-decoration: none;
color: #0098D3;
}
.class2 A:link {
text-decoration: none;
color: #7a7a7a;
font-style: italic;
font-weight: 300;
}
.class2 A:visited {
text-decoration: none;
color: #7a7a7a;
font-style: italic;
font-weight: 300;
}
.class2 A:active {
text-decoration: none;
font-style: italic;
font-weight: 300;
}
.class2 A:hover {
text-decoration: none;
font-style: italic;
color: #0098D3;
font-weight: 300;


</style>



</div> <!-- .et-description -->

<!--<a href="<?php the_permalink(); ?>" class="read-more"><span><?php esc_html_e( 'More>', 'Nexus' ); ?></span></a>-->
</div> <!-- .recent-post -->

<?php
endwhile;

if ( function_exists( 'wp_pagenavi' ) )
wp_pagenavi();
else
get_template_part( 'includes/navigation', 'index' );
else :
get_template_part( 'includes/no-results', 'index' );
endif;
?>
</div> <!-- end #left-area -->
</div> <!-- #content -->

<?php get_sidebar(); ?>
</div> <!-- .main-content-wrap -->

<?php get_template_part( 'includes/footer-banner', 'index' ); ?>
</div> <!-- #main-content -->

<?php get_footer(); ?>


Fahad Murtaza comments:

Don't worry, I can help.


Shane Pennington comments:

Thank you Fahd. Im looking over everything right now and it all looks right. The questionable thing is the author taxonomy is called with "authors" not "author" so I pluralized them but to no avail.


Fahad Murtaza comments:

Hi Shane, can I look into your actual code. I mean can I get access to wherever it is live? If so, I think I can fix quickly. Looks like there is a typo somewhere and its taking time for me to create a local demo on my localhost.


Shane Pennington comments:

From your profile you look pretty reputable. Please don't take offense to it but IDK man that makes me pretty nervous.


Fahad Murtaza comments:

OK :)


Shane Pennington comments:

How about screen sharing where I can see whats going on. I wouldn't have a problem with that. I use espresso.


Shane Pennington comments:

I don't know what screen sharing soft would be the best. I think ichat has screen sharing. Are you on a mac?


Fahad Murtaza comments:

No, I don't prefer screen sharing. I'll see if I can solve this.


Shane Pennington comments:

Ok, Thank you.


Fahad Murtaza comments:

one question. Any particular reason for using index.php instead of taxonomy.php ?


Shane Pennington comments:

No, I didn't think it mattered. And I'm not sure how to tell wordpress to pull the taxonomy.php instead of index.php
here is the site: http://www.quotetweet.com
I guess I could create a taxonomy.php template and assign it to the author page. Are you sure it matters?


Fahad Murtaza comments:

So http://quotetweet.com/authors/a-a-milne/ is an example of a taxonomy page?


Shane Pennington comments:

Well that is using the author taxonomy "A.A.Milne to generate data to the index page.
That is an index page your seeing with taxonomy data on it in other words.


Shane Pennington comments:

I'm sorry have already assigned a taxonomy template to the author page. That is what displays when you choose authors from the top nav. [[LINK href="http://quotetweet.com/authors/ "]][[/LINK]] The theme is coded to display the index.php by default and I'm not sure how to change that, is what I meant to say.


Fahad Murtaza comments:

Well if there is a taxonomy.php, that takes priority over index.php and if there is a taxonomy-authors.php, that takes priority over other two i.e the hierarchy is


taxonomy-authors.php
taxonomy.php
index.php


Usually when you create a new taxonomy, you need to go and save permalinks again.

BTW, the code you used had obvious flaws. You tried mixing the code from one part of article to the second part of it. I am studying it and fixing it right now.



Shane Pennington comments:

Ok I see. I created a page template and assigned it to the author page referencing my author taxonomies.
Would their be any gain by creating an taxonomy-author.php? It would just be an index page with a different name from what I understand.


Fahad Murtaza comments:

Yes, a different taxonomy page for a different taxonomy means you can create a different design for different type of categorisation. Anyhow, if you don't need it, there is no point creating it. That was just a discussion about the taxonomy templates.


I solved your main problem :)

Just use this code anywhere in the post loop. I just wrote plain echos for all variables with line breaks. Here is the code without further ado. I kept the debugging lines but they are commented out.


$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_id = $term->term_id;

//echo $term_id;

$t_id = $term_id;

$term_meta = get_option( "taxonomy_term_$t_id" );

/*
echo "<pre>";
print_r( $term_meta);
echo "</pre>";
*/

extract($term_meta);

echo "<br/>".$photo;

echo "<br/>".$name;

echo "<br/>".$aka;

echo "<br/>".$birth;

echo "<br/>".$birth_place;

echo "<br/>".$death;

echo "<br/>".$star_sign;

echo "<br/>".$hit_movies;

echo "<br/>".$hit_songs;

echo "<br/>".$bio;


Fahad Murtaza comments:

A public GOST for your solution code for easy copy/paste.

[[LINK href="https://gist.github.com/fahdi/7714336"]]https://gist.github.com/fahdi/7714336[[/LINK]]


Fahad Murtaza comments:

// GOST = GIST :)


Shane Pennington comments:

It works! Thank you very much Fahd. Since this is my first time using wpquestions how do I give you credit?


Fahad Murtaza comments:

[[LINK href="http://wpquestions.com/voting/question/id/9039"]]http://wpquestions.com/voting/question/id/9039[[/LINK]] Click on this link and vote to award prize.

Also, the "vote to award prize" link is below your question.

[[LINK href="https://www.dropbox.com/s/gj1a0k43ch2ugjm/Screenshot%202013-11-30%2007.02.22.png"]]https://www.dropbox.com/s/gj1a0k43ch2ugjm/Screenshot%202013-11-30%2007.02.22.png[[/LINK]]


Shane Pennington comments:

Yeah, I tried to vote but said I'm not allowed lol.
Im trying again right now. And thanks again bud, I really appreciate all your effort!


Shane Pennington comments:

Got it.


Shane Pennington comments:

Last night I only looked at 2 authors after I installed your fix. One AA Milne, which looked ok and Abraham Lincoln, which was displaying the correct information. Today I checked the other authors and everyone of them is displaying this error.

Warning: extract() expects parameter 1 to be array, boolean given in /home/quotetwe/public_html/wp-content/themes/Nexus/index.php on line 35


Shane Pennington comments:

It works. Thanks for another fix Fahd.
End result:

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_id = $term->term_id;

$t_id = $term_id;

$term_meta = get_option( "taxonomy_term_$t_id" );

$name= $term_meta['name'];
$aka = $term_meta['aka'];
$photo= $term_meta['photo'];
$birth = $term_meta['birth'];
$birth_place= $term_meta['birth_place'];
$star_sign = $term_meta['star_sign'];
$hit_movies = $term_meta['hit_movies'];
$hit_songs = $term_meta['hit_songs'];
$bio = $term_meta['bio'];
echo $name;
echo $aka;
echo $photo;
echo $birth;
echo $birth_place;
echo $star_sign;
echo $hit_movies;
echo $bio;

?>