Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>';}
?>
share|improve this question
    
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
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
i found my solution, thanks for answer –  Batman Feb 3 '13 at 18:56
    
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
add comment

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;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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