Home Forums General Issues Use custom field of category in WP query


Use custom field of category in WP query

  • Hey,

    i have a custom colorfield in my categories, where i want to save a color and use it for background in a common wp_query.

    <ul class="list-latest <?php echo $columns; ?>">
    						<?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
    						<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    							<a href="<?php the_permalink() ?>">
    								<div class="image">
    									<?php the_post_thumbnail( 'thumb-normal-l', array( 'class' => 'responsive-img' ) ); ?>
    								<div class="content">
    										$category = get_the_category();
    									<span class="category-name" style="background: COLOR HERE ?>">
    										<?php echo $category[0]->cat_name; ?>
    									<span class="post-title"><?php the_title(); ?></span>
    						<?php endwhile;	wp_reset_postdata(); ?>

    I have no ideas, how I can realize that. Anyone any suggestions?

  • Check out the documentation for get_field(), specifically the section titled “Get a value from different objects”. You should be able to get the value from a field assigned to a category by passing the category ID as second parameter:

    $color = get_field( 'my_field', $category[0]->cat_id );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.