• Resolved taela87

    (@taela87)


    Hi,

    I’m trying to display the amount of free and paid posts that are available on my site in the Paid Memberships Pro level.php file, and I have it working for paid, but for free I have to manually change the number whenever I publish a free post, so I’m trying to automate that.

    I think the problem is that I’m using the else statement incorrectly (there’s something missing or maybe I’m replicating the get_term_by too much or something).

    Here’s the code I have that works:

    <?php
    	// Display the amount of articles
      if(pmpro_isLevelFree($level))
    	echo '92';
      else
    	$the_term = get_term_by( 'name', 'article', 'post_tag' );
            echo $the_term->count;
    
    	?>

    For the above code to work, I just add ‘article’ as the post tag to my articles.

    Here’s the code I tried to use to automate that ’92’ echo, but it just stopped the whole page from displaying:

    <?php
    	// Display the amount of articles
      if(pmpro_isLevelFree($level))
    
    	$the_term = get_term_by( 'name', 'free-article', 'post_tag' );
            echo $the_term->count;
      else
    	$the_term = get_term_by( 'name', 'article', 'post_tag' );
            echo $the_term->count;
    
    	?>

    I don’t write PHP very much, but I suspect I’m just not using the else statement correctly, or maybe it’s the hyphenated ‘free-article’ tag that is the problem?

    Any help with getting the code right would be great.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You need some curly braces around your conditionals. So

    <?php
    	// Display the amount of articles
    	if(pmpro_isLevelFree($level))
    	{
    		$the_term = get_term_by( 'name', 'free-article', 'post_tag' );
            	echo $the_term->count;
    	}
    	else
    	{
    		$the_term = get_term_by( 'name', 'article', 'post_tag' );
            	echo $the_term->count;
    	}
    ?>
    Thread Starter taela87

    (@taela87)

    Thanks Jason. That works perfectly.

    Also, thank you for the great plugin.

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

The topic ‘Post count by tag in PMPro levels’ is closed to new replies.