0

I'm trying to get the category of a post that is in 2 different category. One category is always "Featured" and the other changes.

I can only see the word Array()

This is my code:

<?php $args = array( 'numberposts' => '1', 'order' => 'DESC','category' => 17 );
   $recent_posts = wp_get_recent_posts( $args );
//Now lets do something with these posts
foreach( $recent_posts as $recent )
    {
        echo '<div id="testata">';
        echo '<a href="' . get_permalink($recent["ID"]) . '" title="Dettaglio '.esc_attr($recent["post_title"]).'" >'  ;
        echo get_the_post_thumbnail($recent["ID"],"thumb-category") ;
        echo '</a>';
        echo '<div class="testata_inner">';
        echo '<span class="'.get_the_category($recent["cat_name"]) . '">';
        if ($cat[0]->name != "Featured") {
            echo $cat[0]->name;
        } else {
            echo $cat[1]->name;
        }
        echo '</span>';
        echo '<p>'.esc_attr($recent["post_title"]).'</p>';
        echo '<span class="' . esc_attr($recent["post_author"]) . '">' . esc_attr($recent["post_author"]) . '</span>';
        echo '</div> ';
        echo '</div> ';

    } ?>
  • $cat isn't defined. You can't echo an array or you will just get "Array". Try var_dump($your_variable_name) and you should see the structure of your data. – s_ha_dum Oct 23 '14 at 16:47
0

This should get you going in the right direction.

echo '<div class="testata_inner">';
$cat = get_the_category($recent["ID"]); //get the category array
if ($cat[0]->name != "Featured") { //check if the category is "featured"
    $catname = $cat[0]->name; //set $catname variable to "featured"
} else {
    $catname = $cat[1]->name; //or set $catname variable to "other category name"
}
echo '<span class="'. $catname . '">';
echo $catname; //output $catname variable
echo '</span>';

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.