• Hi,

    my website has a category page. I want list of subcategories on this page and I want to display – name of subcategory, description of subcategory and image of subcategory. In ACF I added custom image field (the field name is obrazek_podkategorie and it is supposed to return image URL) for categories and now in my template file category.php I have this:

    <?php
    
    $this_category_id=get_query_var('cat');
    $args=array(
    	'parent' => $this_category_id,
    	'orderby' => 'name',
      	'order' => 'ASC',
      	'hide_empty' => 0
     );
    
    $categories=get_categories($args);
    
    	foreach($categories as $category) {
    		echo '<img src="'. the_field('obrazek_podkategorie') .'"/>';
    		echo '<b>'. $category->name .'</b>';
        	        echo '<p>'. $category->description .'</p>';}
    ?>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    but it doesn’t work..how can I display the image of subcategory? Can anybody help me pls?

    https://www.ads-software.com/extend/plugins/advanced-custom-fields/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter R22

    (@r22)

    ok so I found I need to add get_field funciton in my file, so the code looks like this now:

    <?php
    
    $this_category_id=get_query_var('cat');
    $args=array(
    	'parent' => $this_category_id,
    	'orderby' => 'name',
      	'order' => 'ASC',
      	'hide_empty' => 0
      );
    
    $categories=get_categories($args);
    $image_url=get_field('obrazek_podkategorie', 'category_'. 
    
    $category->cat_ID .'');
    
    	foreach($categories as $category) {
    		echo '<img src="'. $image_url .'"/>';
    		echo '<b>'. $category->name .'</b>';
        	echo '<p>'. $category->description .'</p>';}
    ?>

    but it doesn’t work because (I think) in this function
    get_field('obrazek_podkategorie', 'category_'. $category->cat_ID .'')
    the part
    'category_'. $category->cat_ID .
    doesn’t return category ID..

    how should I fix it? any help pls?

    This works for me:


    <?php

    $this_category_id=get_query_var('cat');
    $args=array(
    'parent' => $this_category_id,
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => 0
    );

    $categories=get_categories($args);

    foreach($categories as $category) {
    $image_url=get_field('fm_category_image', 'category_'.$category->cat_ID);
    echo '<img src="'. $image_url[sizes][large] .'"/>';
    echo '<b>'. $category->name .'</b>';
    echo '<p>'. $category->description .'</p>';}
    ?>

    I added the image size.

    Hy,

    maybe knows someone how return value from custom field via api?

    e.g.

    defined custom field for category (category image), and now,

    i would like return image url via xmlrpc (get_categories method).

    In file class-wp-xmlrpc-server.php i found get_categores structure, and what now?

    How i can insert e.g. $struct[‘image’] = $custom_filed ->custom_filed_value;

    THX a lot

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display custom image field in foreach’ is closed to new replies.