[Support request] Output a loop in WP hook

Home Forums Support [Support request] Output a loop in WP hook

Home Forums Support Output a loop in WP hook

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #598893
    Benedikt

    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).

    #599221
    Leo
    Staff
    Customer Support

    Hi 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.

    #599735
    Benedikt

    Hi 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!
    Bene

    #600171
    Leo
    Staff
    Customer Support
    #600548
    Benedikt

    Okay, 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?

    #600907
    Leo
    Staff
    Customer Support

    Not sure what you mean by sticky posts function?

    I’m not sure about ACF meta values either. Have you tried creating a new list?

    #600931
    Benedikt

    Sorry … 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!

    #600955
    Tom
    Lead Developer
    Lead Developer

    In 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.

    #601301
    Benedikt

    Yeah, 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;
    
    ?>
    #601579
    Tom
    Lead Developer
    Lead Developer

    I would expect the second one to work, as you added the ID into the_permalink() and the_title().

    What if you add: wp_reset_query() before the start of the loop?

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