0

I have a code to get all taxonomies and terms for a single post:

$object_terms = wp_get_object_terms($post->ID, 'staff', array('fields' => 'all'));
if ($object_terms) {
    echo '<p>' . '' . '' ;
    $res = '';
    foreach ($object_terms as $term) {
        $res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View artiles of: %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
    }
    echo rtrim($res,' ,').' ' . '</p>';
}

the code works well when i put it in the single post php file. But when i try to put it in a widget using a special php code widget it shows nothing. I tried other php codes in that widget and they run fine but this didn't. Any help please?

1 Answer 1

0

Try this with or without the return.

    $object_terms = wp_get_object_terms($GLOBALS['post']->ID, 'staff', array('fields' => 'all'));
    if ($object_terms) {
        echo '<p>' . '' . '' ;
        $res = '';
        foreach ($object_terms as $term) {
            $res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View artiles of: %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
        }
        echo rtrim($res,' ,').' ' . '</p>';
    }
10
  • thanks for your response, actually i use a plugin to run php codes in widgets. all other codes are running fine but this didn't. does it really need a separate plugin?
    – nisr
    Commented Jul 28, 2015 at 0:44
  • I updated the answer try wrapping with ob_start(); ob_get_clean();
    – EJD
    Commented Jul 28, 2015 at 1:01
  • nothing happened :(
    – nisr
    Commented Jul 28, 2015 at 1:07
  • Are you sure you get results from the code within the post you are viewing ? Add echo 1; echo 2;... after each line just to make sure that it is at least executing.
    – EJD
    Commented Jul 28, 2015 at 1:09
  • echo 1; $object_terms = wp_get_object_terms($post->ID, 'staff', array('fields' => 'all')); echo 2;
    – EJD
    Commented Jul 28, 2015 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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