Home > Net >  Change the Category Name with ACF inside the loop
Change the Category Name with ACF inside the loop

Time:11-26

I have the following loop:

<?php
    $args = array(
        'post_type' => 'cpt-a',
        'category_name' => 'my-category',
        'posts_per_page' => 99,
        'orderby' => 'date',
        'order' => 'ASC',
        'ignore_sticky_posts' => 1,
        'paged' => $paged);
            $loop = new WP_Query($args);
            if ($loop->have_posts()) :
            while ($loop->have_posts()) : $loop->the_post(); ?>
        
                <div><?php echo get_the_content(); ?></div>
                <span ><?php the_title(); ?></span>

<?php endwhile;
endif;
wp_reset_postdata();
?>

Is it possible to use a custom field (ACF) instead of "my-category"? I tried all the known options, but I could not find a working solution.

CodePudding user response:

You just want to use the value of a variable in the place where there was a static text literal before, but with

'category_name' => '<?php echo $section_reviews['reviews_cat']; ?>'

you have written invalid code. You are inside PHP tags here already, and those can not be nested.

You simply need to put the variable name there,

'category_name' => $section_reviews['reviews_cat'],
  • Related