• Resolved jodybethw

    (@jodybethw)


    I need to add a line at the top of each archive page that states the?post count for that archive page, like this:

    The search “Colorado” yielded 5 articles.

    What is the best way to do this?

    (Please note: I already looked at the “Simple Blog Stats” plugin, which can output the number of total posts on the entire site, which we don’t want, or it can output the post count manually (if I create each archive page and plug in the shortcode for each) but I need to output the number of posts per archive page automatically as we have hundreds of archive pages. If it helps, I am using Elementor Pro and Advanced Custom Fields as well.)

    Thank you!

    • This topic was modified 3 years, 6 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi @jodybethw! You should be able to do this with $GLOBALS['wp_query']->found_posts on the archive page. If you haven’t yet customized your archive page, you can do so by creating an archive.php file. Here is a tutorial on how to go about doing that: Creating an Archive Index.

    Thread Starter jodybethw

    (@jodybethw)

    Thank you so much for replying, @aetherunbound!

    I have an archive page, but don’t know how to add this code. I am using Elementor, which requires that I create a shortcode in functions.php:

    // Shortcode to output custom PHP in Elementor
    function wpc_elementor_shortcode( $atts ) {
        echo "This is my custom PHP output in Elementor!";
    }
    
    add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');

    Where should I place $GLOBALS['wp_query']->found_posts?

    • This reply was modified 3 years, 6 months ago by jodybethw.
    • This reply was modified 3 years, 6 months ago by jodybethw.
    • This reply was modified 3 years, 6 months ago by jodybethw.

    Awesome, that’s most of what we’ll need. I think you might be able to do something like this:

    // Shortcode to output custom PHP in Elementor
    function wpc_elementor_shortcode( $atts ) {
        $post_count = $GLOBALS['wp_query']->found_posts;
        echo "Total posts: " . $post_count;
    }
    add_shortcode( 'my_elementor_php_output', 'wpc_elementor_shortcode');
    

    The echo piece is what will create the text seen on the page, so that’s where we want to put the found posts! You can change the text in quotes to whatever you think is best.

    Thread Starter jodybethw

    (@jodybethw)

    OMG @aetherunbound you’re a genius! So close. So if I want it to say, for instance:

    The search “Colorado” yielded 5 articles.

    How do I add text before and after the post count? And how do I automatically input the tag for each archive page?

    • This reply was modified 3 years, 6 months ago by jodybethw.
    Thread Starter jodybethw

    (@jodybethw)

    I figured out how to add a second echo to add “articles” at the end. So I just need help with inputting the tags/categories. Some of the archive pages are for categories, some have tags, and other are custom taxonomies. Ugh.

    • This reply was modified 3 years, 6 months ago by jodybethw.

    Check out this guide! It might have some of what you need (specifically the “Making the category and tag archive output dynamic” section): https://vegibit.com/what-is-a-wordpress-archive-page/

    Instead of the <?php block, you could use the results of single_cat_title and single_tag_title in a variable and add it to the echo statement just as you’re doing with $post_count.

    Thread Starter jodybethw

    (@jodybethw)

    Thanks. One big problem:

    Search results are being included in this. So when I search for the word “who” on the site, I get this output:

    The search “Search Results for: who” yielded
    545 articles

    I tried removing “Search Results for:” from search/php but it sin’t do anything. Is there a way to exclude search results from this function? I’ve tried to find info on this and I’m getting nowhere. I’m not being paid to do this, and I’m tearing my hair out. I REALLY appreciate your help!

    Forgive me, but do you mind describing what you would like to see and what you’re currently seeing a bit more? Based off your original question, said you wanted to see something like this:

    The search “Colorado” yielded 5 articles.

    It sounds like that’s what you’re currently getting (or something similar), is that correct?

    Thread Starter jodybethw

    (@jodybethw)

    Oh you know what? I just realized that this search issue is an Elementor problem. Not related to what you wee helping me with. Nevermind! Thank you SO much for your help!

    No problem! I’m glad I was able to help ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to show post count on archive pages’ is closed to new replies.