• Resolved Obje

    (@harrow)


    Hi,

    I’ve recently dive into Custom Post Types and I’m struggling with taxonomies.

    In fact, I create a CPT (‘project’) and its associated taxonomy (‘color’). I also created an archive template for the CPT (‘archive-project.php’) and a ‘taxonomy-color.php’ file to display the projects regarding the ‘color’taxonomy they belong. Then I created different posts with using ‘blue’ and ‘red’ terms.

    At this point all is ok. Then I’m trying to display different content (a simple text) at the and of the page if I’m on https://localhost:8888/test/color/blue/ or https://localhost:8888/test/color/red/

    I’ve found out a solution using basic php in my ‘taxonomy-color.php’ file like so :

    <?php if ( is_tax('color','blue' ) ) {?>
        <div class="test" style="color: blue">Hello</div>
    <?php } elseif ( is_tax('color','red'  ) ) {?>
        <div class="test" style="color: red">Hello</div>
    <?php } ?>

    But it’s not really dynamic, so I had a look at the very useful ACF plugin to create a custom field and assign it to the ‘color’ taxonomy. All is ok on the admin side but I can’t find a way to display the value of the custom fields in my template file. The different ways I’ve used fail.

    Is anybody has an idea or a different point of view to display different content on a taxonomy archive template?

    Thanks by advance for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi harrow,

    have you checked this page https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/ of ACF plugin ? i think it will solve your problem.

    -Swayam

    Thread Starter Obje

    (@harrow)

    Hi Swayam,

    Thanks for your answer. Off course, I spend 1 hour on this page ! and I really don’t understand how to make it works !

    The plugin’s creator says :

    Instead of the above string, a term object can also be passed as the $post_id parameter as of ACF v4.3.3. You can get a term object via many of WP’s functions such as get_term.

    So I understand I can’t just use <?php the_field(‘field_name’, $term); ?> as is, like any custom field code.

    I’ve tried something like :

    <?php $term = get_term( $term_id, $taxonomy ); ?>
     <p><?php the_field('price', $term); ?></p>

    But as a beginner in php, I don’t really understand what is $term_id and $ taxonomy in my case…

    Thank for your help.

    ok, plz post full code of “taxonomy-color.php”, so i can take a look and help you with.

    Thread Starter Obje

    (@harrow)

    Thanks a lot for your help.

    <?php get_header(); ?>
    <div class="projects">
    
    <?php if ( is_tax('color','blue' ) ) {?>
        <div class="test" style="color: blue">Hello</div>
    <?php } elseif ( is_tax('color','red') ) {?>
        <div class="test" style="color: red">Hello</div>
    <?php } ?>
    
      <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
          <article class="projet">
            <?php the_post_thumbnail( 'thumbnail' ); ?>
            <h1 class="title">
              <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
              </a>
            </h1>
            <div class="content">
              <?php the_content(); ?>
            </div>
    // this part fails
            <p><?php the_field('price', $term); ?></p>
    
          </article>
        <?php endwhile; ?>
      <?php endif; ?>
    </div>
    
    <?php get_footer(); ?>

    ‘price’ is the custom field I’ve created into ACF and I assigned it to “Taxonomy term” => “Colors” (label name)

    this works for me

    <?php get_header(); ?>
        <div class="projects">
    
            <?php if ( is_tax('color','blue' ) ) {?>
                <div class="test" style="color: blue">Hello</div>
            <?php } elseif ( is_tax('color','red') ) {?>
                <div class="test" style="color: red">Hello</div>
            <?php } ?>
    
            <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>
                    <article class="projet">
                        <?php the_post_thumbnail( 'thumbnail' ); ?>
                        <h1 class="title">
                            <a href="<?php the_permalink(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h1>
                        <div class="content">
                            <?php the_content(); ?>
                        </div>
    
                        <?php
    
                        global $wp_query;
    
                        $term = $wp_query->queried_object;
    
                        $price = get_field('price', $term);
    
                        echo $price;
    
                        ?>
    
                    </article>
                <?php endwhile; ?>
            <?php endif; ?>
    
        </div>
    
    <?php get_footer(); ?>
    Thread Starter Obje

    (@harrow)

    Buddy,

    You saved my day !

    Thanks a lot

    you’re welcome ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Post Type & Taxonomy – Custom fields in archive template’ is closed to new replies.