Support

Account

Home Forums General Issues Category values to containing posts

Solving

Category values to containing posts

  • Hello! I am working on the following problem:

    I have a WordPress Portfolio with some categories and posts in them.

    I added additional fields to these categories and now can assign a logo and a color to the category. Now I want the system to recognize that posts are in a certain category and retrieve the values (logo and color) from the parent category.

    Is there an easy way to do that?

    Cheers, Markus

  • Easy is a relative term.

    You need to alter the loop that displays the post(s). Use wp_get_object_terms() or possibly wp_get_post_terms() and then get the values from the associated term(s) get_field('field_name', $term).

  • Hey John, thanks for your support. I read a lot about this topic, but can’t make it work. So maybe some general questions would help.

    It is possible to pass the values of a category to the posts in this category, right? Would be nice if e.g. the category has a company logo, the posts in this category could also have the company logo.

    Thing is I built the site with a theme which has the funcionality to add the values of a custom field to templates, posts, categorys etc., but it only finds an shows the values if you for example put a “post-custom-field” in a post or post-template. You can put a custom field of it’s category in the post, but the field will be empty then. So I have to somehow make the system be able to hand the values from category to the posts in this category.

    Do you think this would be possbile through functions.php or do I have to built custom theme files anyway?

    Would be nice to clear up things in my head a little bit. 🙂

    Thanks for your help!

  • All I can to you tell you how this would be done in PHP. If you’re using some type of page builder theme then you need to contact them.

    
    // example, loop on single post page
    if (have_posts()) {
      while (have_posts()) {
        the_post();
        $terms = get_post_terms($post->ID, 'your-taxonomy');
        if ($terms) {
          $term = $terms[0];
          $image = get_field('image_field_name', $term);
        }
      }
    }
    
  • Hi John,

    would this also be possible via functions.php?

    e.g. with wp_get_post_terms??

  • Hello again John,

    I tried your code in my single.php, which I assume should be the right place. MY category is us_portfolio_category and just to try a more simple field I took “email” which should just put out the plain text. But I can’t make it work. Field stays empty.

    I thought there mighty be an easy way to pass category values through to it’s posts. :/

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

You must be logged in to reply to this topic.