• Amit Singh

    (@amitsksingh)


    let me explain my requirements. I have wordpress website and I have created a custom post type reviews. Single post have acf fields which I want to show up depending upon the the term user clicks on taxonomy archive page. So under custom post type review I have taxonomy with slug vendor_category and under this taxonomy I have three terms with slug business-banking, business-line-of-credit and business-loans. Now what I want is that if user click on business banking acf field to be shown is banking_field and if user click on business-line-of-credit then acf field to be shown is line_of_credit and if user click on business-loans then acf field to be shown is business_loans_field. Also one post is assigned to all terms so information showed on single post depend upon the clicked term

Viewing 8 replies - 1 through 8 (of 8 total)
  • Amit Dholiya

    (@amitdholiya1990)

    Custom Post Type and Taxonomy Setup: Ensure your custom post type (reviews) and taxonomy (vendor_category) are correctly registered.

    ACF Fields: Make sure your ACF fields (banking_field, line_of_credit, business_loans_field) are created and assigned to the reviews post type.

    Query and Display ACF Fields: You can achieve this in your taxonomy template file (e.g., taxonomy-vendor_category.php).

    Here’s a simplified version of the code you might use in your taxonomy template:

    <?php
    // Check if we are on the taxonomy archive
    if (is_tax('vendor_category')) {
    // Get the current term
    $current_term = get_queried_object();

    // Get all posts of the custom post type 'reviews' for the current term
    $args = array(
    'post_type' => 'reviews',
    'tax_query' => array(
    array(
    'taxonomy' => 'vendor_category',
    'field' => 'slug',
    'terms' => $current_term->slug,
    ),
    ),
    );

    $reviews_query = new WP_Query($args);

    if ($reviews_query->have_posts()) {
    while ($reviews_query->have_posts()) {
    $reviews_query->the_post();

    // Get the ACF fields based on the current term
    switch ($current_term->slug) {
    case 'business-banking':
    $field_to_show = get_field('banking_field');
    break;
    case 'business-line-of-credit':
    $field_to_show = get_field('line_of_credit');
    break;
    case 'business-loans':
    $field_to_show = get_field('business_loans_field');
    break;
    default:
    $field_to_show = null;
    break;
    }

    // Output the field
    if ($field_to_show) {
    echo '<div class="acf-field">' . esc_html($field_to_show) . '</div>';
    }
    }
    wp_reset_postdata();
    } else {
    echo '<p>No reviews found for this category.</p>';
    }
    }
    ?>
    graphicscove

    (@graphicscove)

    Hey Amit,

    You could try adding the taxonomy term to the link like so:

    <a href="<?php the_permalink(); ?>?term=<?php echo get_queried_object()->slug; ?>">
    <?php the_title(); ?>
    </a>

    Then you can get this on your single-reviews.php page and conditionally render the fields:

    $term_slug = isset($_GET['term']) ? sanitize_text_field($_GET['term']) : '';

    if ($term_slug === 'business-banking') {
    the_field('banking_field');
    } elseif ($term_slug === 'business-line-of-credit') {
    the_field('line_of_credit');
    } elseif ($term_slug === 'business-loans') {
    the_field('business_loans_field');
    }

    Hopefully that helps or points you in the right direction!

    Thread Starter Amit Singh

    (@amitsksingh)

    @graphicscove Thanks a lot for your responding. I implemented this and its working perfectly but I have one issue with this. When I click on any post title in URL parameters are showing like term=business-banking etc. Is there any way to hide this. I dont want to show parameters in URL.

    Once again many thanks for your help. Regards, Amit

    graphicscove

    (@graphicscove)

    @amitsksingh Here’s another way to handle this without exposing the term in the URL by using javascript:

    <a href="<?php the_permalink(); ?>" class="term-link" data-term="<?php echo get_queried_object()->slug; ?>">
    <?php the_title(); ?>
    </a>

    <script>
    document.addEventListener('DOMContentLoaded', function() {
    const termLinks = document.querySelectorAll('.term-link');

    termLinks.forEach(link => {
    link.addEventListener('click', function(event) {
    const term = this.getAttribute('data-term');
    localStorage.setItem('selectedTerm', term);
    });
    });
    });
    </script>

    And then on your single page:

    <script>
    document.addEventListener('DOMContentLoaded', function() {
    const selectedTerm = localStorage.getItem('selectedTerm');

    if (selectedTerm === 'business-banking') {
    <?php the_field('banking_field'); ?>
    } else if (selectedTerm === 'business-line-of-credit') {
    <?php the_field('line_of_credit'); ?>
    } else if (selectedTerm === 'business-loans') {
    <?php the_field('business_loans_field'); ?>
    }

    // Optionally clear the local storage once used
    localStorage.removeItem('selectedTerm');
    });
    </script>
    Thread Starter Amit Singh

    (@amitsksingh)

    @graphicscove After adding this. Single page shows blank page

    Thread Starter Amit Singh

    (@amitsksingh)

    @amitdholiya1990 Thanks a lot but its not working.

    graphicscove

    (@graphicscove)

    @amitsksingh Appologies, I’m typing this without having WordPress set up to test at the moment.

    Could you try this instead?

    <div class="acf-field-container">
    <script>
    document.addEventListener('DOMContentLoaded', function() {
    const selectedTerm = localStorage.getItem('selectedTerm');

    if (selectedTerm === 'business-banking') {
    document.querySelector('.acf-field-container').innerHTML =
    <?php the_field('banking_field'); ?>;
    } else if (selectedTerm === 'business-line-of-credit') {
    document.querySelector('.acf-field-container').innerHTML = <?php the_field('line_of_credit'); ?>;
    } else if (selectedTerm === 'business-loans') {
    document.querySelector('.acf-field-container').innerHTML = <?php the_field('business_loans_field'); ?>;
    }

    // Optionally clear the local storage once used
    localStorage.removeItem('selectedTerm');
    });
    </script>
    </div>
    Thread Starter Amit Singh

    (@amitsksingh)

    @graphicscove Thanks for your great help. With small changes I got what i was looking for. Thanks for your great help. Regards, Amit

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.