0
votes

So I want to return a Wordpress loop that returns just one category, so currently I have something like this

<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

This works great and does exactly what I want it to do

But I want the category number to be set by an ACF, so I have that set up and it's returning my value just as a string on the site, all good again, so now my code looks like this

<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

What I want to do is something like this

<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('"cat=", the_field("category_number")'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

I don't think I'm going about this the right way and can't seem to find an answer for this as I don't think I'm looking for the right thing .. if someone could point me in the right direction on this I'd be really grateful

2

2 Answers

0
votes

Use get_the_field instead of the_field in query posts method something like this

query_posts('cat=get_the_field("category_number")');

note:- the_field() is echo everything which field contain.

get_the_field is only get the field not echo it.

Hope it will help you :)

0
votes

Have you tried something along these line?

https://wordpress.stackexchange.com/questions/127940/use-advance-custom-field-inside-query-post-command

<?php query_posts('cat=<?php the_field("category_number"); ?>'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php the_content(); ?>
<?php endwhile; endif; ?>

Or Maybe something like this?

https://www.advancedcustomfields.com/resources/query-posts-custom-fields/