• Treebeard

    (@malawimama)


    This is an advanced question, I’m not really sure where to start, but I’m hoping an advanced programmer might know the answer to this one.

    I need to create either a plain text field or dropdown menu with an ACF field in the backend, then incorporate it into an array in the template.

    The goal is to show posts on a PAGE by selecting the category (or typing the catgory slug) to show (for instance, show posts from Category One on a regular page).

    This is my code:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $args = array(
                'category_name' => 'MyCategoryChoice',
                'paged' => $paged
            );
    
            $list_of_posts = new WP_Query( $args );
            ?>
    
            <?php if ( $list_of_posts->have_posts() ) : ?>
    			<?php /* The loop */ ?>
    			<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
    				<?php // Display content of posts ?>
    				<?php get_template_part( 'content', 'home' ); ?>
    			<?php endwhile; ?>

    I need to replace the ‘MyCategoryChoice’ with a ACF field, but it’s not working.

    I tried this, but it didn’t work…

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $mycategory = the_field('selected_category');
    
            $args = array(
                'category_name' => $mycategory,
                'paged' => $paged
            );

    where ‘selected_category’ is the field name… All that did was show the category name as plain text.

    Any ideas out there? My brain hurts already ??

    Thanks!

    https://www.ads-software.com/plugins/advanced-custom-fields/

Viewing 3 replies - 1 through 3 (of 3 total)
  • mukesh.l

    (@mukeshl)

    Hello Treebeard

    can you plz show the url and then show me what you need because if you want acf field in array then it is possible but i am not understood your requirment so give more information.

    Thanks

    Thread Starter Treebeard

    (@malawimama)

    The URL is a website in development, so I feel reluctant to post it in public because my client’s link would end up being listed in search engine results (that’s happened in the past and you can’t delete these forum posts later).

    What I’m trying to do is allow the client to type a category slug into a plain text field which is used in the array above to show the posts from that category – in a Page (so he can add regular content above the category posts, that’s why we need to use Pages).

    I have the code working without the ACF field like this:

    <?php
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
    					the_content();
    					wp_reset_postdata();
    
                endwhile;
            endif;
    
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $args = array(
                // Change these category SLUGS to suit your use.
                'category_name' => 'my-category-slug',
                'paged' => $paged
            );
    
            $list_of_posts = new WP_Query( $args );
            ?>
    
            <?php if ( $list_of_posts->have_posts() ) : ?>
    			<?php /* The loop */ ?>
    			<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
    				<?php // Display content of posts ?>
    				<?php get_template_part( 'content', 'home' ); ?>
    			<?php endwhile; ?>

    where ‘my-category-slug’ is the category slug used to display the posts from that category.

    But I don’t want the client to have to create separate template files for each page, though, so I was hoping to use a ACF field to allow him to add the category from the admin instead.

    In the code above, and the ACF field works, but only shows plain text on the frontend – instead of showing just the posts from the chosen category, it shows all the posts and shows just plain text on the frontend like this:

    my-category-slug

    above the posts. So the ACF field doesn’t work as I had hoped in this instance. It only shows the actual text that’s typed into the text field, rather than showing the posts from the category “my-category-slug”.

    I hope that explains what I’m trying to achieve. Can you help?

    Thread Starter Treebeard

    (@malawimama)

    I worked on this some more and instead of using a textfield, I switched to a Relationship field, but apparently you can’t select Categories using this method? All I can see is the option to select actual posts to show on the frontend, which is tedious if you have a lot of posts.

    I referenced the ACF documentation here:

    Resources > Relationship

    and created this code instead:

    <?php
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
    					the_content();
    					wp_reset_postdata();
    
                endwhile;
            endif;
    
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $args = array(
                'paged' => $paged
            );
    
            $list_of_posts = new WP_Query( $args );
            ?>
    
    <?php 
    
    $posts = get_field('selected_category');
    
    if( $posts ): ?>
    			<?php foreach( $posts as $post): ?>
            <?php setup_postdata($post); ?>
    				<?php ?>
    				<?php get_template_part( 'content', 'home' ); ?>
    				<?php endforeach; ?>
    				<?php wp_reset_postdata(); ?>
    				<?php endif; ?>

    the template_part is loading a specific layout, but it works fine. I just wish there was a way to use Relationship fields to choose an entire Category instead of having to dig through Posts manually.

    Does anyone know of a way to do that?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use ACF field as part of an array’ is closed to new replies.