- This topic has 13 replies, 3 voices, and was last updated 6 months, 2 weeks ago by
David.
-
AuthorPosts
-
September 7, 2022 at 9:22 am #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 shownSeptember 7, 2022 at 5:56 pm #2336452Fernando 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
.September 7, 2022 at 6:27 pm #2336458riz
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.
September 7, 2022 at 9:31 pm #2336525Fernando 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.
September 7, 2022 at 10:58 pm #2336569riz
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.
September 8, 2022 at 12:48 am #2336650Fernando Customer Support
What hook are you using to add this?
September 8, 2022 at 1:04 am #2336662riz
I added it to the function.php file. I used the shortcode on the “Page Hero” Element type and it’s hooked “After Header”
September 8, 2022 at 1:56 am #2336718Fernando Customer Support
Can you try adding
$var_dump($file);
at the end of your code?September 8, 2022 at 2:54 am #2336773riz
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); } );
September 8, 2022 at 4:19 am #2336855David
StaffCustomer SupportHi there,
the
downloadable_url_link_1
are these fields attached to the Taxonomy ?Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 8, 2022 at 5:03 am #2336917riz
Hi David,
Yes, they are.
September 8, 2022 at 5:13 am #2336926David
StaffCustomer SupportOK, so you need to use the
get_queried_object()
in yourget_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/
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 8, 2022 at 5:23 am #2336931riz
Awesome!
Thank you, David!
It totally works!September 8, 2022 at 5:26 am #2336933David
StaffCustomer SupportGlad to hear that!
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.