[Resolved] Custom Post Type Archive Bugs with ACF Integration

Home Forums Support [Resolved] Custom Post Type Archive Bugs with ACF Integration

Home Forums Support Custom Post Type Archive Bugs with ACF Integration

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #2336230
    riz

    Hi There,

    Is there anyone who has the same problem?
    I built a Custom Post Type Taxonomy Archive and then created a hero element with Block Element for every taxonomy archive. Inside the Hero Element I added two ACF fields. The first field is an image that changes the hero’s background, it’s rendered with dynamic background feature from GenerateBlocks. The second one is a clickable/downloadable file (File Field), it’s rendered with a shortcode.

    The problem
    If taxonomy doesn’t have a post, the background image is not shown. However, when the post is existing the hero background will be shown. Another bug is the File Field. It works well when there is no post, but it’s not working (is not shown) when the post exists. I don’t know where the bugs are coming from.

    So in short

    Post doesn’t exist = Image is not shown, and files are shown.
    Post exist = Image is shown, and Files are not shown

    #2336452
    Fernando
    Customer Support

    Hi Riz,

    That’s the default behavior. It’s not a bug. When there’s no posts in an archive page, the template used is no-results.php.

    #2336458
    riz

    Hi Fernando,

    Thank you for the quick reply.
    Okay, I think the problem for the empty posts in the archive is solved now.

    But what about the ACF field that is not shown when the posts exist in the archive?
    As this condition says: “Post exist = Image is shown, and Files are not shown”

    My goal is for the Files (from ACF) to be shown in the archive when the posts exist.

    #2336525
    Fernando
    Customer Support

    What shortcode are you using?

    It might be good to ask ACF with regards to this, as it should render as expected if you’re running their code. GP has no code that should hinder this from working.

    #2336569
    riz

    I never thought it’s an ACF problem. Since GenerateBlocks only supports the text field on GP elements, so I try to create a shortcode to render the ACF.

    I use this:

    add_shortcode( 'file', function() {
        ob_start();
    	$file = get_field('downloadable_url_link_1');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_1); ?><br><br></a>
    	<?php endif;
    	
    	$file = get_field('downloadable_url_link_2');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_2); ?><br><br></a>
    	<?php endif;
    	
    	$file = get_field('downloadable_url_link_3');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_3); ?><br><br></a>
    	<?php endif;
    	
    	$file = get_field('downloadable_url_link_4');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_4); ?><br><br></a>
    	<?php endif;
    	
    	$link = get_field('downloadable_url_external_link_url');
    if( $link ): ?>
        <a href="<?php echo esc_url( $link ); ?>"><?php the_field(downloadable_url_external_link_text); ?><br><br></a>
    <?php endif;
        return ob_get_clean();
    } );

    It works well when there is no post on the archive page. But when the post exists, this Shortcode is not working, not even displayed as Shortcode, it’s totally disappeared.

    #2336650
    Fernando
    Customer Support

    What hook are you using to add this?

    #2336662
    riz

    I added it to the function.php file. I used the shortcode on the “Page Hero” Element type and it’s hooked “After Header”

    #2336718
    Fernando
    Customer Support

    Can you try adding $var_dump($file); at the end of your code?

    #2336773
    riz

    Okay, I added it inside the shortcode. But the var dump result is also not shown.

    Please correct me if I am wrong.

    add_shortcode( 'file', function() {
        ob_start();
    	$file = get_field('downloadable_url_link_1');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_1); ?><br><br></a>
    	<?php endif;
    	
    	$file = get_field('downloadable_url_link_2');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_2); ?><br><br></a>
    	<?php endif;
    	
    	$file = get_field('downloadable_url_link_3');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_3); ?><br><br></a>
    	<?php endif;
    	
    	$file = get_field('downloadable_url_link_4');
    	if( $file ): ?>
    		<a href="<?php echo $file['url']; ?>"><?php the_field(downloadable_url_link_text_4); ?><br><br></a>
    	<?php endif;
    	
    	$link = get_field('downloadable_url_external_link_url');
    if( $link ): ?>
        <a href="<?php echo esc_url( $link ); ?>"><?php the_field(downloadable_url_external_link_text); ?><br><br></a>
    <?php endif;
        return ob_get_clean();
    	$var_dump($file);
    } );
    #2336855
    David
    Staff
    Customer Support

    Hi there,

    the downloadable_url_link_1 are these fields attached to the Taxonomy ?

    #2336917
    riz

    Hi David,

    Yes, they are.

    #2336926
    David
    Staff
    Customer Support

    OK, so you need to use the get_queried_object() in your get_field to make sure you target the term. Otherwise it will get the ID of the first post in your loop.

    For example:

    
    $term = get_queried_object();
    $image = get_field('downloadable_url_link_1, $term);

    See here for more info:

    https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    #2336931
    riz

    Awesome!
    Thank you, David!
    It totally works!

    #2336933
    David
    Staff
    Customer Support

    Glad to hear that!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.