$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to check if a shortcode is being executed in a widget or post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to check if a shortcode is being executed in a widget or post

matteradmin8PV0评论

I am creating a feature for my website using a WordPress shortcode. The shortcode is displays an HTML table that contains certain data.

I have enabled the short code for widget/sidebar and it is working fine in the widget.

The only problem is that my designer has created two different styles; one for post and another for widget. Is there any way to know that the code is being executed in sidebar or in post?

eg:

if(is_widget()){
//add stylesheet for widget here
}
else{
//add stylesheet for post here
}

Thank you

I am creating a feature for my website using a WordPress shortcode. The shortcode is displays an HTML table that contains certain data.

I have enabled the short code for widget/sidebar and it is working fine in the widget.

The only problem is that my designer has created two different styles; one for post and another for widget. Is there any way to know that the code is being executed in sidebar or in post?

eg:

if(is_widget()){
//add stylesheet for widget here
}
else{
//add stylesheet for post here
}

Thank you

Share Improve this question edited Dec 28, 2016 at 15:50 Jami Gibbs 7694 silver badges13 bronze badges asked Dec 28, 2016 at 15:40 SD433SD433 631 silver badge10 bronze badges 9
  • Are the styles so significantly different that you couldn't just adjust it based on container size? – Jami Gibbs Commented Dec 28, 2016 at 16:00
  • yes, in fact they are two different css files. for post the table have 4 columns and for widget it have 2 columns. – SD433 Commented Dec 28, 2016 at 16:03
  • The Number of columns are just an example, even the icon used are different for widget and post. – SD433 Commented Dec 28, 2016 at 16:11
  • 1 Just brainstorming some ideas (I'm not sure if this is an option depending on who your end-user will be) but what about adding another attribute to your shortcode like [myshortcode type="post"] where type indicates if it's a "post" or "widget" format and then apply a style based on that attribute setting. – Jami Gibbs Commented Dec 28, 2016 at 16:19
  • 1 just add styles for both and use descendant selectors. widgets will be inside widget container elements, and posts will be inside posts container element. – Milo Commented Dec 28, 2016 at 18:14
 |  Show 4 more comments

1 Answer 1

Reset to default 1

Using conditional tag in_the_loop inside your shortcode function may serve the purpose.

if( in_the_loop() ) {
    //add stylesheet for post/page here...
} else {
    //add stylesheet for widget here...
}
Post a comment

comment list (0)

  1. No comments so far