<?php echo do_shortcode('[x_recent_posts type="post" count="2" offset="" category="Lisbon" orientation="vertical" no_sticky="true" no_image="false" fade="false"]');

This is the shortcode, and I need to get category name and place it in shortcode. How can I achieve that?

  • Erm, the code you added completely changes the question - in the future please be more clear from the start. – TheDeadMedic Dec 11 '16 at 15:57
  • I know, and I am really sorry. I have posted this on mobile, and pasted the code, but afrer reading your question I have realsied that code isn't showing. When I edited my question code was there, and I needed to add it as code in text editor. I apologize! – David Dec 11 '16 at 16:20
  • So when you say "get category name", you mean the name of the current category archive? (if it is one) – TheDeadMedic Dec 11 '16 at 16:37
  • Exactly. Theme has that shortcode and I need to place category name in it. It would be great if I can get some variable that will get category name and I can place it shortcode so when user goes to category it shows posts from that category with that shortcode. Thanks a lot in advance! – David Dec 11 '16 at 17:04

So, you can get the category name with get_queried_object()->name:

if ( is_category() ) {
    echo do_shortcode( '[x_recent_posts type="post" count="2" offset="" category="' . get_queried_object()->name . '" orientation="vertical" no_sticky="true" no_image="false" fade="false"]' );

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.