Phantom
Phantom

Reputation: 108

Trouble retrieving custom taxonomy meta field value in single.php

I have looked around and I am unable to see anything that can help me retrieve a custom meta field value in single.php. Below is what i have tried thus far, but i am unsuccessful:

<?php
$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'taxonomy' => 'locations',
  ); 
$terms = get_the_terms( $post->ID, 'category' );
$term_meta = get_option( "taxonomy_$terms->term_id" );  
$categories = get_categories($args);
  foreach($categories as $category) { 
    echo '<p><strong>' . $category->name . '</strong></p> ';
    echo '<p>' . $category->description . '</p>';
    echo '<p>' . echo $term_meta['custom_term_meta'] . '</p>';
} 
?>  

The expected result should look like this:

New York (name)
212 Broadway, NY NY 10001 (description)
212-123-1234 (custom taxonomy meta field value)

Upvotes: 0

Views: 81

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

Chances are that it's the curly quotes ‘ ’ and “ ” causing the problem, which should have thrown an error, had error reporting been enabled and displayed.

Change them to ' and " respectively:

$args = array(
'name',
'order' => 'ASC',
'taxonomy' => 'locations',
);
$terms = get_the_terms( $post->ID, 'category' );
$term_meta = get_option( "taxonomy_$terms->term_id" );
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>' . $category->name . '</p>';
echo '<p>' . $category->description . '</p>';
echo '<p>' . echo $term_meta['custom_term_meta'] . '</p>';
}

Add error reporting to the top of your file(s) which will help finding errors.

Sidenote: Error reporting should only be done in staging, and never production.

error_reporting(E_ALL);
ini_set('display_errors', 1);
  • Thanks to Daniel Cooley for his input on this.

This was most likely caused by using MS Word or similar word processing software.

Upvotes: 1

Related Questions