0

I have a site where I've got a post type called 'small banners' which are basically adverts that i need to call on normal pages (This is fine, I've done this) but I also need to use this functionality on a Taxonomy term.

ACF doesn't have the functionality to select taxonomy terms themselves just to filter the posts within them to select.

How can I do this?

This is my code for calling the adverts on a normal page -

<div id="small-banner-top">
                <?php 


                    $smallbanners = get_posts(array(
                        'post_type' => 'small-banner',
                        'posts_per_page' => '-1',
                        'meta_query' => array(
                            array(
                                'key' => 'show_on_post', // name of custom field
                                'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                                'compare' => 'LIKE'
                            )
                        )
                    ));

                ?>


                         <div class="my-slider" style="width: 380px;">
                         <ul>

                            <?php if( $smallbanners ): ?>

                            <?php foreach( $smallbanners as $smallbanner ): ?>
                            <?php 

                            $photo = get_field('small_banner_image', $smallbanner->ID);

                            $bannerlink = get_field('url', $smallbanner->ID);

                            ?>



                            <li>
                                <a href="<?php echo $bannerlink; ?>">
                                    <img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>">

                                </a>
                            </li>


                        <?php endforeach; ?>

                    <?php endif; ?>
                        </ul>
                    </div>

    </div>

So far I've just used the taxonomy field option and selected which taxonomy terms I'd like it to show on using this and used this code on a custom page called taxonomy-directory_entry_type.php (the taxonomy terms are called directory entry types).

<?php 

                    /*
                    *  Query posts for a relationship value.
                    *  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
                    */

                    $smallbanners = get_posts(array(
                        'post_type' => 'small-banner',
                        'posts_per_page' => '-1',
                        'meta_query' => array(
                            array(
                                'key' => 'show_on_tax', // name of custom field

                                'compare' => 'LIKE'
                            )
                        )
                    ));

                    ?>


                         <div class="my-slider" style="width: 380px;">
                         <ul>

                    <?php if( $smallbanners ): ?>

                        <?php foreach( $smallbanners as $smallbanner ): ?>
                            <?php 

                            $photo = get_field('small_banner_image', $smallbanner->ID);

                            $bannerlink = get_field('url', $smallbanner->ID);

                            ?>




    <li>
        <a href="<?php echo $bannerlink; ?>">
            <img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>">
        </a>
    </li>











                        <?php endforeach; ?>

                    <?php endif; ?>
                        </ul>
                    </div>

This is bringing in all the adverts but I'm struggling on how to create the logic for it to only bring in the selected adverts. Any help would be much appreciated,

Thanks!

0

1 Answer 1

1

This is based on the assumption that you have a custom field called "show_on_tax" for the custom taxonomy "Directory Entry Type", which appears when editing a small banner (See image below). This allows you to select which custom tax terms you want the banners to appear on. This should get you sorted but drop a comment if not.

<?php 
// taxonomy-directory_entry_type.php template file

// Get currently viewed term id
$term_id = get_queried_object()->term_id;

// Get all banners with show_on_tax custom field matching to $term_id
$args = array(
    'post_type' => 'small-banner',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'show_on_tax',
            'value' => $term_id,
            'compare' => 'LIKE'
        )
    )
);

// Create custom banner query
$banner_query = new WP_Query( $args );

// Check if anything is found and do the custom loop
if ( $banner_query->have_posts() ) : ?>

    <div class="my-slider" style="width: 380px;">
        <ul>

        <?php while ( $banner_query->have_posts() ) : $banner_query->the_post(); ?>

            <?php $photo = get_field( 'small_banner_image', get_the_ID() ); ?>

            <li>
                <a href="<?php the_field( 'small_banner_image' ); ?>">
                    <img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>">
                </a>
            </li>

        <?php endwhile; ?>

        </ul>
    </div>

<?php
endif;

wp_reset_postdata();
?>

Custom field settings

1
  • That's brilliant, thank you so much. that's worked perfectly! I see what I was doing wrong now. I really appreciate the time you've taken to help me, thank you
    – Lucy Brown
    Commented Jan 26, 2017 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.