Page with all categories with featured image

Discussion in 'WordPress' started by Divvy, Jan 15, 2023.

  1. #1
    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 :D
    https://www.advancedcustomfields.com/resources/image/

    Can someone help me, please?

    Thank you in advance!
     
    Divvy, Jan 15, 2023 IP
  2. snaroliya

    snaroliya Active Member

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #2
    print_r($image);

    and check which array key having the ctg image url.
     
    snaroliya, Jan 15, 2023 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    The result is NULL
     
    Divvy, Jan 16, 2023 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,307
    Likes Received:
    1,702
    Best Answers:
    31
    Trophy Points:
    475
    #4
    Try:
    $image = get_field('cat_thumbnail_image', $category->term_id );
    instead of
    $image = get_field('image', $category->term_id);
     
    qwikad.com, Jan 16, 2023 IP
  5. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    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
     
    Divvy, Jan 16, 2023 IP
  6. snaroliya

    snaroliya Active Member

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #6
    Check the category image slug at ACF and use the same here:

    $image = get_field('image', $category->term_id);
     
    snaroliya, Jan 16, 2023 IP
  7. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #7
    Divvy, Jan 16, 2023 IP
  8. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #8
    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
     
    Divvy, Jan 16, 2023 IP
  9. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #9
    Solved! I tried with a different name and is now working perfectly! :)
    Thank you all for your time helping me!
     
    Divvy, Jan 16, 2023 IP
  10. snaroliya

    snaroliya Active Member

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #10
    Great!
     
    snaroliya, Jan 16, 2023 IP
  11. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #11
    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? :p

    
    <?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.
     
    Divvy, Jan 16, 2023 IP
  12. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #12
    Already solved this with:
    'orderby' => 'include',
    'include' => array( 10, 2, 5 ), //exact order id
    Code (markup):
     
    Divvy, Jan 17, 2023 IP