0

I'm struggling with substituting a variable for a hard-coded value in a shortcode.

I'm using the following shortcode (written by Ahmed Fouad --thanks Ahmed) at this link which counts the # of posts in a category.

function category_post_count( $atts ) {
$atts = shortcode_atts( array(
    'category' => null
), $atts );
$term = get_term_by( 'slug', $atts['category'], 'category');
return ( isset( $term->count ) ) ? $term->count : 0; 
}
add_shortcode( 'category_post_count', 'category_post_count' );

It works great and I get the correct count, provided I hard code the name of the category, e.g. "computers" below:

<?php echo do_shortcode('[category_post_count category="computers"]'); ?>

I want the shortcode to automatically insert the name of the category. So I used the following to grab the category's slug (from Rachel Baker at this link -- thanks, Rachel):

$category = get_the_category(); 
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id != 0 ) {
  $category_parent = get_term( $category_parent_id, 'category' );
  $css_slug = $category_parent->slug;
} else {
  $css_slug = $category[0]->slug;
}

This, too, works great and gets the correct category name ("computers"). But when I insert the $css_slug variable into the shortcode, like this, I get a zero count:

<?php echo do_shortcode('[category_post_count category=$css_slug]'); ?>

What am I doing incorrectly with the assignment? Thanks in advance.

1

Try this:

<?php echo do_shortcode('[category_post_count category='.$css_slug.']'); ?>

I'm not sure what you're trying to do though? If you're not doing this in actual content pages, you don't need the shortcode.

You could just do it all within the php page. Let me know if you need more info.

  • Thank you for your prompt reply, rudtek. That didn't work, unfortunately. I'm trying to display a bulleted list of titles of other posts related to this post's category(s), limited to 10, at the bottom of a single-content.php page. And display nothing if there are no related-category posts. It seemed pretty simple at first but I'm a Wordpress/PHP 'noob' and I've been all over the map for days. I appreciate any help you can provide. – Andreas3204 Feb 5 '18 at 23:22
  • what is the error when you put that code in? – rudtek Feb 5 '18 at 23:25
  • Sorry, I'm an idiot. It does work (been looking at it so long, that I didn't put the quotes around '.$css_slug.' as you had). Thank you, rudtek. Works great. I like your suggestion of not using a shortcode to get the category count -- but don't know how to write that. Is that pretty simple? (Reason I need the counts is to ignore displaying related-category posts if count <= 1.) – Andreas3204 Feb 5 '18 at 23:41
  • glad it worked! mind marking as answered? It would be a bit of work, but if what you have is working, you're good to go. – rudtek Feb 5 '18 at 23:53
  • It works like a champ! You're a gem, rudtek. Thank you so, so much. :) – Andreas3204 Feb 6 '18 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.