Appending a field in the categories array
-
Hi folks,
This must be basic PHP thing but my PHP knowledge is even poorer than that. I am trying to have 4 thumbnails (featured images) of random posts in the same category so I used this:
<?php $posts = get_posts('orderby=rand&numberposts=4&cat=category_id'); foreach($posts as $post) { ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a> <?php } ?>
However my problem is that every post is in two categories and I need the code above to ignore second category.
For this piece of code
<?php $categories = get_the_category($post->ID); var_dump($categories);
I get the following:
array (size=2) 0 => object(stdClass)[211] public 'term_id' => &string '38' (length=2) public 'name' => &string 'Advertising' (length=11) public 'slug' => &string 'advertising-2' (length=13) public 'term_group' => string '0' (length=1) public 'term_taxonomy_id' => string '43' (length=2) public 'taxonomy' => string 'category' (length=8) public 'description' => &string '' (length=0) public 'parent' => &string '0' (length=1) public 'count' => &string '2' (length=1) public 'object_id' => string '248' (length=3) public 'cat_ID' => &string '38' (length=2) public 'category_count' => &string '2' (length=1) public 'category_description' => &string '' (length=0) public 'cat_name' => &string 'Advertising' (length=11) public 'category_nicename' => &string 'advertising-2' (length=13) public 'category_parent' => &string '0' (length=1) 1 => object(stdClass)[212] public 'term_id' => &string '29' (length=2) public 'name' => &string 'All Logos' (length=9) public 'slug' => &string 'all' (length=3) public 'term_group' => string '0' (length=1) public 'term_taxonomy_id' => string '34' (length=2) public 'taxonomy' => string 'category' (length=8) public 'description' => &string '' (length=0) public 'parent' => &string '0' (length=1) public 'count' => &string '24' (length=2) public 'object_id' => string '248' (length=3) public 'cat_ID' => &string '29' (length=2) public 'category_count' => &string '24' (length=2) public 'category_description' => &string '' (length=0) public 'cat_name' => &string 'All Logos' (length=9) public 'category_nicename' => &string 'all' (length=3) public 'category_parent' => &string '0' (length=1)
How do I get cat_ID from the first object, please?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Appending a field in the categories array’ is closed to new replies.