WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display number of posts by user, only if they have any. (6 posts)

  1. Shunarjuna
    Member
    Posted 1 year ago #

    I am using a plugin shortcode to display a list of blog posts by users on their own custom post type (sort of a user profile page).
    I am using this code to display the number of posts
    <h3><?php the_author(); ?> has contributed <?php the_author_posts(); ?> articles to the blog</h3>
    and below that goes the shortcode.
    How do I display this only if the user has made contributions to the blog. At the moment it says "(User) has contributed 0 articles to the blog" or "(User) has contributed 1 articles to the blog"
    I would also like it to use the singular "article" if the number is 1.
    Samples: http://www.teachmemusic.com.au/listings/more-test/
    http://www.teachmemusic.com.au/listings/test-required-fields/

    about half way down the page.
    Thanks.

  2. michael.mariart
    Member
    Posted 1 year ago #

    Change the function that you're using, and it will work the way that you want it to (with a few small logic changes added).

    <?php $articles = get_the_author_posts(); ?>
    <?php if ($articles > 0): ?>
        <h3><?php the_author(); ?> has contributed <?php echo $articles; ?> articles to the blog</h3>
    <?php endif; ?>
  3. Shunarjuna
    Member
    Posted 1 year ago #

    That's excellent. Thank you so much. You're a champion!

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    I would also like it to use the singular "article" if the number is 1.

    adapt the one line:

    <h3><?php the_author(); ?> has contributed <?php echo $articles; ?> article<?php echo ($articles != 1?'s':''); ?> to the blog</h3>
  5. Shunarjuna
    Member
    Posted 1 year ago #

    Wow, that's perfect now. You rock.
    I was actually using elseif then repeating the line with "article" instead of "articles"
    Your way is much much better, thank you.

  6. Shunarjuna
    Member
    Posted 1 year ago #

    Just closing the thread.

Topic Closed

This topic has been closed to new replies.

About this Topic