0

I use a wordpress plugin in which i can add custom data for each category Now because of some error it doesn't always grab the correct data. to fix that i wrote the following:

<?php if ( in_category( 3 ) ) : 
?>

<?php the_field( "categoryselect", "category_3" ); ?>

<?php endif; ?>

Now that works, but is there a way to loop through all my categories with this?
(basicly i need the "3" and the number in "category_3" to loop through all my categories) i couldnt get it to work, so was going to hard code it, but thought i would ask for help before hardcoding it :P

Thnx in advance

Edit, with help from Ketan, i came to this solution that works for my problem

<?php 
$category = get_queried_object();
$testtest = $category->term_id;
if ( in_category( $testtest ) ) : 
?>

<?php the_field( "categoryselect", "category_".$testtest ); ?>

<?php endif; ?>

1 Answer 1

1

You can do something like this:

<?php
for($i = 0; $i < categories_count; $i++) {
    if ( in_category( $i ) ) :
        the_field( "categoryselect", "category_".$i );
    endif;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thnx for looking into it for me. For some reason this didnt work in my case. but pretty sure that has more to do with the 10.000 errors already present in this theme :')
thnx!. the_field( "categoryselect", "category_".$i ); that part gave me an idea how to solve this issue.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.