- This topic has 9 replies, 3 voices, and was last updated 5 years, 9 months ago by Tom.
-
AuthorPosts
-
June 13, 2018 at 4:20 am #598893Benedikt
Hey GP community!
I am currently working on my first GP Pro project. It incorporates ACF, WooCommerce and, of course, GP pro. So far, I’m quite pleased with how it works.Right now, though, I ran into a problem:
For a general FAQ page, I want to give the client the ability to add some “featured posts“ at the end of the FAQ main page, like they do here: https://helpcenter.mymuesli.com/hc/de
So I created an ACF field for this site which collects post objects (from a custom post type) with a repeater field and try to output it after content via GP Hooks. So far, the “after content“ hook didn’t work, but the “before footer“ does which is fine. But …The code which I pulled from the ACF doc here builds a loop and, strangely, it only puts out the title and href of the current page, not that of the linked posts. Do loops work inside Hooks, or would it be better to create a page template php file like ‘page-fragen.php’ and put in the code there? So far I liked the idea of the hooks and wanted to avoid diving too much into the code.
I’d appreciate any help and can send you the login credentials to see the site via mail (sorry, unf. I can’t make them public).
June 13, 2018 at 9:27 am #599221LeoStaffCustomer SupportHi there,
Might be easier to use Tom’s WP Show Posts plugin to generate the list of posts you want and then just add them in hooks using shortcode:
https://en-ca.wordpress.org/plugins/wp-show-posts/Let me know if this helps.
June 14, 2018 at 1:49 am #599735BenediktHi Leo,
thanks – I actually already thought about that. But would it be possible to restrict the output of the list to only this single page when I put the shortcode into the hook? Otherwise, I’d have to put it into the content, right?
And, is it possible to filter the post list by an ACF meta value? I’d like to have a „popular post“ checkbox per post.
Thanks!
BeneJune 14, 2018 at 9:42 am #600171LeoStaffCustomer SupportYup you’d just have to use some conditional tag:
https://docs.generatepress.com/article/using-hooks-conditional-tags/
https://codex.wordpress.org/Conditional_Tags#A_PAGE_PageLet me know 🙂
June 15, 2018 at 1:57 am #600548BenediktOkay, now you know I‘m a php noob 😀 Thanks for that!
But how could I filter the listed posts with some kind of sticky post function? Is WP Show Posts able to filter via ACF meta values?June 15, 2018 at 9:07 am #600907LeoStaffCustomer SupportNot sure what you mean by sticky posts function?
I’m not sure about ACF meta values either. Have you tried creating a new list?
June 15, 2018 at 9:37 am #600931BenediktSorry … I’ll clarify that:
If I’d build a list for regular post, I’d try to filter for „Sticky post“, so, by marking a post as „sticky“, the content editor can choose if it’s in the list or not. But as far as I learned, Custom Post Types don’t have a „Sticky Post“ function. So I’d need another way for him to choose if a post gets displayed in this list or not. And as far as I saw in the videos (haven’t bought the pro version yet), I saw that there’s some option for meta value queries. So I thought I just give all FAQ posts a checkbox which reads „Show as featured post“ and of course this has to be filtered by the WP Show Posts list. So if I can work this out via tie Pro Version of the plugin and ACF, I guess I’d have to invest 😉
Thanks for your help, Leo!
I really apprecciate that!June 15, 2018 at 10:06 am #600955TomLead DeveloperLead DeveloperIn your original post you mentioned pulling code from the ACF docs. Can you share that code with me? I might be able to spot the issue.
Adding loops within loops can become problematic like this sometimes.
June 16, 2018 at 1:26 am #601301BenediktYeah, basically I pulled the two code snippets below from the ACF doc and tried it with both:
https://www.advancedcustomfields.com/resources/post-object/empfohlener_beitrag is my post type which is built via a repetition field. See:
https://www.dropbox.com/s/pe10rq2dbdj9pcl/empfohlener_beitrag.png?dl=0<?php /* * Loop through post objects (assuming this is a multi-select field) ( setup postdata ) * Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop * Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset */ $post_objects = get_field('empfohlene_beitrage'); if( $post_objects ): ?> <ul> <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Post Object Custom Field: <?php the_field('empfohlener_beitrag'); ?></span> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; /* * Loop through post objects (assuming this is a multi-select field) ( don't setup postdata ) * Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question. */ $post_objects = get_field('empfohlene_beitrage'); if( $post_objects ): ?> <ul> <?php foreach( $post_objects as $post_object): ?> <li> <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a> <span>Post Object Custom Field: <?php the_field('empfohlener_beitrag', $post_object->ID); ?></span> </li> <?php endforeach; ?> </ul> <?php endif; ?>
June 16, 2018 at 9:26 am #601579TomLead DeveloperLead DeveloperI would expect the second one to work, as you added the ID into
the_permalink()
andthe_title()
.What if you add:
wp_reset_query()
before the start of the loop? -
AuthorPosts
- You must be logged in to reply to this topic.