Support

Account

Home Forums General Issues How to show category information on single post

Solved

How to show category information on single post

  • Hi all

    I’m creating my first WordPress site and my inexperience is getting in the way.

    I’m creating a news site about products, there may be several different articles about the same product and there will be many different products.

    Each product will be set up as a category. On the article post page I would like a sidebar with general information about the product. This information is coming from custom fields in the category.

    The location rule is set up as Taxonomy is equal to Category. Then I add an image on the category page, but am stumped as to how I add this to the template. The code needs to know the category of the post and then get the information from the appropriate category.

    Any help is much appreciated, I hope I have explained myself well enough, apologies if not. PHP is totally new to me.

  • I completely forgot to give an example of what I’m trying to achieve. So if the article is about a film for example, let’s say Casablanca, I want the sidebar to have generic information that I filled out in the category creation like an image of a DVD cover for Casablanca and I want that to be true of all articles I write about Casablanca. This code should grab that image right?

    <?php $prodimage = get_field( 'prodimage' ); ?>
    <?php if ( $prodimage ) { ?>
    	<img src="<?php echo $prodimage['url']; ?>" alt="<?php echo $prodimage['alt']; ?>" />
    <?php } ?>

    I don’t know how to only pull the prodimage for the category of the post like the following code that pulls in the worpdress category description for the post:

    <?php $catID = get_the_category(); echo category_description( $catID[0] ); ?>

  • You need to specify the id value that ACF needs to get the field https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    
    <?php 
      $categories= get_the_category();
      if (!empty($categories)) {
        $term_id = $categories[0]->term_id;
        $image = get_field('category-image-field', 'term_'.$term_id);
        // what you do from here depends on what the field is returning
      }
    ?>
    
  • This solved my issue, many thanks!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.