[wp-hackers] loading css only on page where plugin is used

Aaron D. Campbell aaron at xavisys.com
Thu Apr 23 00:20:22 GMT 2009


I mean using the WP_Query class to create a query that is NOT stored in 
the $wp_query global.  For example:

$queryArgs = array(
    'nopaging'        => true,
    'post_type'        => 'any',
    'post_status'    => 'draft,pending,future,publish'
);
$myQuery = new WP_Query($queryArgs);
if ($myQuery->have_posts()) {
    while ($myQuery->have_posts()) {
        $post = $myQuery->next_post();
        // Do something with $post
    }
}


aesqe wrote:
> sorry for asking what might seem like a pretty dumb question, but what 
> kind of custom query? could you give an example? :/
>
> On 22.4.2009 19:18, Aaron D. Campbell wrote:
>> That will only work with the default queries, if someone does a custom
>> query it won't catch it. I'd recommend the inline idea and just set a
>> flag so the styles are only added once.
>>
>> aesqe wrote:
>>> Michael, try using something like this:
>>>
>>> function shortcode_css()
>>> {
>>> global $wp_query;
>>> if( !empty($wp_query->posts) )
>>> {
>>> foreach( $wp_query->posts as $post )
>>> {
>>> if(preg_match("#\[your_shortcode[^\]]*\]#is", $post->post_content)
>>> wp_enqueue_style( "your css name", "your css location" );
>>> }
>>> }
>>> }
>>> add_action('wp_print_styles', 'shortcode_css');
>>>
>>> On 22.4.2009 13:38, Michael Toppa wrote:
>>>> Hi all,
>>>>
>>>> I'm working on a plugin that uses a shortcode. I want to load a css 
>>>> file
>>>> for the plugin only for posts or pages that use the shortcode.



More information about the wp-hackers mailing list