Support

Account

Home Forums Add-ons Repeater Field Only show post if subfield has certain value

Solving

Only show post if subfield has certain value

  • I have an advanced custom sub field taxonomy repeater. I would only like it display the data if the post has a category number of 8. And not post any of the other posts which have a different category number.

    Here is my code:

    <?php
    
        if( get_field('team_profile') ): ?>
            <?php while( has_sub_field('team_profile' ) ): ?>
    
                <div>
                    <?php if( get_sub_field('category', 8) ): ?>
                        <ul>
                        <?php while( has_sub_field('category') ): ?>
                            <li>
                                <?php 
    
                                $variable = get_sub_field('category',8);
    
                                echo 'I should only be printing posts if i have a value of 8';
    
                                ?>
                            </li>
                        <?php endwhile; ?>
                        </ul>
                    <?php endif; ?>
                </div>  
    
            <?php endwhile; ?>
        <?php endif; ?>
  • I need some clarification what you are trying to do with your code above…

    Because this: <?php if( get_sub_field(‘category’, 8) ): ?> …means that you are trying to check if post ID 8 has a sub field ‘category’.

    If this is not what you want but instead want to see if sub_field ‘category’ has the value 8 you can do it like this:

    <?php
    $variable = get_sub_field(‘category’);
    if ($variable && $variable == ‘8’) {
    echo ‘Yes this post has a field category with value 8’;
    }
    ?>

    But as I’m not fully aware of your situation, I’m also thinking that you may be confusing actual wordpress categories and custom fields and if that’s the case, you code needs a whole different approach.

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

The topic ‘Only show post if subfield has certain value’ is closed to new replies.