• pk-71

    (@pauljohnknight)


    Hi,

    I’m building a custom theme and would like to pin the 4th post on an archive page for a custom post type. How would I go about doing this? When you Google this subject it is just a sea of articles about how to pin the 1st post. The reason for wanting to pin the fourth post is because it is going to contain an advertisement / sponsored link.

    Obviously I wish to pin it so it stays in fourth place not pin that post to the top of the page.

    I have been using the WP_Query() function on the archive page itself in case it is possible with this function (I can’t see that it is though).

    Many thanks in advance for any pointers / advice.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There are a few ways to accomplish this. I’d insert the ad post at the 4th position independent of the query results. Modify the loop in the archive template to include a loop counter. When the counter equals 4, get and output the ad post. This will be in addition to the posts per page count of archive lists. Adjust accordingly if that’s significant. This ad will occur on every page of the list unless you also check for the $paged variable.

    For this to only happen for a CPT archive list, create a template just for the CPT named "archive-{$post_type}.php". Create a child theme to contain this file if you’ve not already done so.

    Instead of a loop counter, you could fiddle with the WP_Query::results array, inserting the post at the desired location. This can be done with the global $wp_query object before the loop starts.

    Either way, the inserted post would appear again in the archive if is part of the main query. You may want to explicitly exclude the ad post from the general query to avoid this. This would be done through the “pre_get_posts” action hook.

    Thread Starter pk-71

    (@pauljohnknight)

    Hi @bcworkz

    Thanks for this. Because I’m building a custom theme from scratch I already have an archive-{$post_type}.php file, which is archive-news.php. In terms of using the WP_Query() you mention at the end…I can’t see anywhere in the docs how to do this?

    Here is a simplified version of how I use the WP query object. I can’t seem to see in the docs though what would go where the first // CODE GOES HERE comment to achieve what I wish to achieve?

        <?php 
            $archivePageNews = new WP_Query(array(
                
                // CODE GOES HERE
    
            ));
    
            while(  $archivePageNews->have_posts()){
                    $archivePageNews->the_post(); ?>
    
                    <article class="article top-article">
                    
                    <!-- ARCHIVE POST CONTENT GOES HERE  -->
                    
                    </article>
    
        <?php } ?>
        
        <?php  wp_reset_postdata(); ?>
    
    • This reply was modified 5 years, 1 month ago by pk-71.
    • This reply was modified 5 years, 1 month ago by pk-71.
    • This reply was modified 5 years, 1 month ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    There are no arguments available to a new WP_Query object that would allow you to arbitrarily insert a post at the 4th position. Your code would need to manipulate the query results after the query has obtained the results. Results would be in $archivePageNews->results. This approach is a little hacky IMO, I’d advise adding a loop counter instead. If you wish to modify the posts per page count over the default, add something like 'posts_per_page' => 9, to // CODE GOES HERE.

    The posts per page limits posts on a page, but typical pagination functions don’t work with custom queries. You need to pass the current page requested through 'paged' => $currentpage, where you’ve somehow assigned the requested page to $currentpage.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pin the 4th Post On The Archive Page Of A Custom Post Type’ is closed to new replies.