1
votes

I have a Portfolio on my website built with Wordpress custom post types and Advanced custom fields - I currently have this bit of code, looping through the 'brand' category of my portfolio:

<?php
        $portfolio_args = array(
            'post_type' => 'portfolio',
            'portfolio-category' => 'brand',
            'posts_per_page' => -1
        );

        $portfolio = new WP_Query($portfolio_args);

        while($portfolio->have_posts()) {
            $portfolio->the_post();
            $post = new SeedPost(get_the_ID());
            $post->display(true);
        }
        wp_reset_query();
        ?>

This works fine, but I would like to bring in the category name dynamically from a custom field... So, this is the line I think I should edit...

'portfolio-category' => 'brand',

I have tried the following two options but neither seem to work:

'portfolio-category' => '<?php the_field('category_to_show'); ?>',

'portfolio-category' => 'the_field('category_to_show');',

I understand the first option probably doesn't work because I have more PHP tags in there so I removed them for the second attempt - Still isn't working though - Can anyone help?

1

1 Answers

1
votes

You should be able to get what you need like this:

$portfolio_args = array(
    'post_type' => 'portfolio',
    'portfolio-category' => get_field('category_to_show'),
    'posts_per_page' => -1
);

The get_field() function returns the value of the specified field.

Hope this helps...

Ref: https://www.advancedcustomfields.com/resources/get_field/