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?

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>';
    }
|improve this answer|||||
  • 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 Jul 28 '15 at 0:44
  • I updated the answer try wrapping with ob_start(); ob_get_clean(); – EJD Jul 28 '15 at 1:01
  • nothing happened :( – nisr Jul 28 '15 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 Jul 28 '15 at 1:09
  • echo 1; $object_terms = wp_get_object_terms($post->ID, 'staff', array('fields' => 'all')); echo 2; – EJD Jul 28 '15 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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