1

How can i retrive the categories from a post_type that i select.

I have a Custom_field - categorie_serial

<?php

$values = get_field('categorie_serial');
if($values)
{
    echo '<ul>';

    foreach($values as $value)
    {
        echo '<li>' . $value . '</li>';
    }

    echo '</ul>';
}

// always good to see exactly what you are working with
var_dump($values);

?>

This code retrive me the categories but it retrive me an error The line 40 is "foreach($values as $value)"

    Warning: Invalid argument supplied for foreach() in /home/****/public_html/****/wp-content/themes/movies/tabserial/detali-serial.php on line 40

string(334) "Gen SerialeActiuneAnimatieAventura" 

How can i retrive the only the parent categories? I do not want to retrive Gen Seriale - categoriy , only Actiune..


RESOLVED:

<?php 
$terms = get_terms('seriale','child_of=668');foreach ($terms as $term) {echo '<a href="'.get_term_link($term).'">'.$term->name.'</a>';}
?>
  • why are you saving categories as a string in a meta field? – Milo Feb 3 '13 at 17:40
  • hy milo please see up, i updated, i found a solution in wp codex, now how can i filter to display only the parent of a category? – Batman Feb 3 '13 at 17:45
  • Good that you found a solution. However, it's better to post it as an answer (you're allowed - and even encouraged - to answer you're own question) – Lea Cohen Feb 3 '13 at 19:39
2

This should get you started

function my_cpt_cats() {
    $parent = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    $args = array(
        'type'                     => 'post', 
        'child_of'                 => $parent->term_id,
        'parent'                   => '',
        'orderby'                  => 'slug',
        'order'                    => 'DESC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'YOUR_TAXONOMY',
        'pad_counts'               => false 
    );
    $categories = get_categories( $args );
}

Remember to use your taxonomy name in the $args array.

| improve this answer | |
  • sorry for the quck response , my solution did not work, after i added another post it in archive it apears duplicate – Batman Feb 4 '13 at 0:30
0

The solution for other. Thanks to Milo.

668 - is the id of the category

$terms = get_the_terms( $post->ID, 'seriale' );
foreach( $terms as $term ):
    if( 668 == $term->parent ){
        echo '<a href="'.get_term_link($term).'">'.$term->name.'</a>';
    }
endforeach;
| improve this answer | |

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.