Hello guys, I'm trying to create a page to show all the categories, with the cat name and featured image (acf). My PHP is very poor and I can't do this myself, can someone help me with this, please? This is what I'm doing: I have a page to show posts from a custom post type. I'm using the same code to show the categories instead of the posts, and for that I'm changing some lines of the code. Here's what I did so far: <?php $args = array( 'orderby' => 'id', 'hide_empty'=> 0, ); $categories = get_categories($args); $size = 'full'; foreach($categories as $category) { $image = get_field('image', $category->term_id); echo ' <li> <a href="' . get_category_link($category->term_id) . '"><img src="'.$image['url'].'" alt="'.$image['alt']. '"/> <span class="crp_title" style="bottom:4px;">', ( strlen($title= the_title('', '', false)) > 48 ? substr($title, 0, 48) : $category->name ), '</span></a> </li>'; } echo '</ul>'; echo '<div class="crp_clear"></div></div>'; ?> PHP: Everything is working perfectly, the only thing that is missing is the category image URL. For the image, I'm using the ACF plugin. I've read this but I don't know how to use the code in mine. This is probably very simple, but as I said, I'm too newbie https://www.advancedcustomfields.com/resources/image/ Can someone help me, please? Thank you in advance!
Try: $image = get_field('cat_thumbnail_image', $category->term_id ); instead of $image = get_field('image', $category->term_id);
Thank you, but unfortunately, didn't work. Btw, I'm using this code in a page template, do you think is because of that? Page -> Page Attributes -> Template Here's the full code: https://paste2.org/1h3ebwy5
Check the category image slug at ACF and use the same here: $image = get_field('image', $category->term_id);
Didn't work either, I already have the "image" as a slug: https://prnt.sc/vwTtPE1UFF2t Rules: https://prnt.sc/L2wI0evEaYRM Page: https://prnt.sc/V96ex_RV9Qu_
I found out that if I replace this code: <? $args = array( 'orderby' => 'id', 'hide_empty'=> 0, ); $categories = get_categories($args); $size = 'full'; foreach($categories as $category) { $image = get_field('image', $category->term_id); //var_dump($image); print_r($image); echo ' <li> <a href="' . get_category_link($category->term_id) . '"><img src="'.$image['url'].'" alt="' . $category->name . '" /></a> <span class="crp_title" style="bottom:4px;">', ( strlen($title= the_title('', '', false)) > 48 ? substr($title, 0, 48) : $category->name ), '</span> </li>'; } echo '</ul>'; echo '<div class="crp_clear"></div></div>'; ?> Code (markup): With this: <?php $args = ['hide_empty' => 0,];$cats = get_categories($args);foreach ($cats as $cat) {$image = get_field('image', $cat);print_r($image);}?> Code (markup): Appears this result: https://paste2.org/a34F9c2t
Solved! I tried with a different name and is now working perfectly! Thank you all for your time helping me!
Btw, does someone know how to get_categories() result by a predetermined order? I want to return an array, based on the order in which category IDs are included. Something like: $categories = array(); $cat_ids = array(5,4,7,6); foreach ( $cat_ids as $id ) { $categories[] = get_category( $id ); } Code (markup): But how to modify my code to work like this? <?php $args = ['hide_empty' => 0,]; $cats = get_categories($args); foreach ($cats as $cat) { $image = get_field('cat_image', $cat); echo ' <li> <a href="' . get_category_link($cat->term_id) . '"><img src="'.$image['url'].'" alt="' . $cat->name . '" /></a> <span class="crp_title" style="bottom:4px;">', ( strlen($title= the_title('', '', false)) > 48 ? substr($title, 0, 48) : $cat->name ), '</span> </li>'; }?> Code (markup): I'm already using a plugin to order the categories by custom order, I’m using it for the main page only. The current order is the plugin order, but I want to choose what specific categories I want to show first in a specific page using the code above.
Already solved this with: 'orderby' => 'include', 'include' => array( 10, 2, 5 ), //exact order id Code (markup):