Forums

Give help. Get help.

  • # November 29, 2017 at 5:08 pm

    Hey! I’ve been searching high and low for weeks and still haven’t been able to get this to work properly.

    I’m building an archive page template that needs to display the displayed Posts “Number” within it’s Custom Post Type (not the number within the category or term). So, if there are 41 posts in my CPT, then publishing a new Post would make it Post #42. I need to get that number “42” in a variable for use in my template – outside of the loop. I’m displaying these posts with Ajax Load More using their “repeater template” where my PHP to display the post is. But my code isn’t working properly.

    I was able to make it work using the Stack Overflow question below, within the Loop directly on my Archive page template:
    https://stackoverflow.com/questions/36719609/get-the-current-post-number-not-id-in-wordpress

    However, I’m now forced to use the Ajax Load More plugin, which is forcing me to use their “repeater templates”, where that snippet no longer works:

    $podcast_number = $wp_query->current_post + 1;
    

    I’m assuming it’s because they aren’t using $wp_query as the variable to query the posts.

    Another approach I thought about – Since I’m using ACF, I thought I’d create a custom field and populate it via PHP (in functions.php) automatically, but again, that’s not working properly. Here’s my code there:

    <?php
    
    function my_acf_load_field( $field ) {
    
         global $wp_query;
    
         $podcast_number = $wp_query->current_post + 1;
    
         $field['value'] = $podcast_number;// Value
         $field['readonly'] = true;
    
         return $field;
    
    }
    
    add_filter('acf/load_field/name=podcast_number', 'my_acf_load_field');
    
    ?>
    

    In my post for that custom field, it just displays “0”. I want it to display “42” (based on my example above for this specific new post). I’m guessing it has something to do with not being in the loop. I also couldn’t get a clear idea of what the Ajax Load More plugin uses as their search variable, since the repeater template should be in the loop, but I’m unsure what to use to hook into it.

    Thank you for any help!

    # December 13, 2017 at 8:43 pm

    Thank you for this post it helped me too.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag