0

I'm trying to do something that seems really simple but I can't figure it out.

In my category template, I have this shortcode:

<?php echo do_shortcode( '[jobs categories="manufacturing"]' ); ?>

I also have this to show the title of the category:

<?php single_cat_title(); ?>

I would like to put them both together so I can pass the category into the shortcode so it fetches the correct job category listings. So I created this:

<?php $category = single_cat_title(); ?>
<?php echo do_shortcode('[jobs categories=' . $category . ']'); ?>

but it doesn't seem to work properly - Am I doing something wrong? Is this even possible?

Many thanks!!

10
  • 1
    Why does it not "seem to work properly"? What is it doing wrong? Your first example of single_cat_title() doesn't imply that it returns anything, but your second example assumes that it does. Does it? Commented Jun 17, 2018 at 17:01
  • <?php single_cat_title(); ?> returns the name of the current category at the top of the page... Commented Jun 17, 2018 at 17:03
  • That line of code isn't capturing a return value. It implies that the function prints to the output, but not returns anything. Do you have any way to get the value in code and not just print it to the output? Commented Jun 17, 2018 at 17:04
  • Sorry, David, I'm not entirely sure what you mean - I'm a beginner at this so I'm lost with the terminology currently... IT outputs the title and the shortcode outputs the job board showing the manufacturing jobs in that case - both bots of code work separately on the page so they both output things correctly? Commented Jun 17, 2018 at 17:09
  • There's a difference between outputting and returning. The single_cat_title() function appears to output data to the browser, but the function itself isn't returning anything (or at least not what you expect) to code which calls that function. This is where some introductory PHP tutorials will be helpful for you. Focus on the structure of building and using functions. You need to get the value in your code, not just on the page. Commented Jun 17, 2018 at 17:11

1 Answer 1

2

If you look at the documentation for the single_cat_title() function, you'll see that it can either display or return the value, and it accepts two parameters $prefix and $display with default values of '' and true respectively.

What this means, is that just using single_cat_title(); will print Category Title to the document.

If you want to use it as a variable (without printing it to the document), you'll need to set the second parameter to false:

$category = single_cat_title( '', false );

This will define the $category variable for you, without printing anything, and you can then pass it to your shortcode (also note, that typically you'll want quotes around your attribute values in shortcodes):

echo do_shortcode( '[jobs categories="' . $category . '"]' );

You can make it a bit more succinct as well:

<?php
    $category = single_cat_title( '', false );
    echo do_shortcode( "[jobs categories='$category']" );
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok - thank you for explaining things in an easy to understand way, that's really helpful :) That would explain why it was printing to the document above the shortcode instead of in the shortcode!! I did also wonder if I could contain it in one set of PHP tags! This also means I can now achive what im trying with one page template instead of a template for each taxonomy! Thanks again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.