Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:10:23+00:00 2026-05-28T04:10:23+00:00

In WordPress I’m trying to enqueue a script for a jQuery gallery only for

  • 0

In WordPress I’m trying to enqueue a script for a jQuery gallery only for posts that got attachments.

I got this simple function that works for single posts:

function gotImages()
{
    $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
    return ( !empty($attachments) ? true : false );
}

When I call gotImages() in functions.php it returns true for a single post with attachments but false for the home page with several posts if the first post doesn’t have attachments.

How can I modify this so it works for more than one post i.e. on the home page where I’m displaying 10 posts?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:10:24+00:00Added an answer on May 28, 2026 at 4:10 am

    Finally worked out a rough working solution. It’s probably not the final version but here’s my solution for anyone who might be interested.

    function gotImages()
    {
        global $wp_query;
        $posts = $wp_query->posts;
    
        if ( empty( $posts ) )
        return $posts;
    
        $searchImages = '~<img [^>]* />~';
    
        foreach ($posts as $post) {
            $content = $post->post_content;
            preg_match_all( $searchImages, $content, $countImages );
            $images += count($countImages[0]);
        }
        return ( $images >= 1 ? true : false );
    }
    

    This function if put in functions.php will read through the content of the current loop and scan it for <IMG> tags. If one or more tags are found it returns true otherwise false.

    Now I can use this to conditionally enqueue scripts or stylesheets depending on if a post got any images.

    For example like this:

    if( !is_admin() && gotImages() == true ) :
        wp_register_script('fancybox', get_stylesheet_directory_uri() . '/libs/fancybox/jquery.fancybox.pack.js', array('jquery'), false, true);
        wp_register_script('fancybox.init', get_stylesheet_directory_uri() . '/js/init/lightbox.init', array('fancybox'), false, true);
    
        wp_enqueue_script('fancybox');
        wp_enqueue_script('fancybox.init');
    endif;
    

    I’m not sure yet how ‘expensive’ this function is but the site I’m working on it quite small so for now it’s not a big deal of it adds a few ms to the process time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wordpress has a filter that automatically add paragraphs to posts. I can remove this
I've got a WordPress powered blog that I'm trying to get setup on our
Has Wordpress a function or something like that?I need a way to check if
In Wordpress, I'm trying to add the post ID of posts I publish in
Wordpress has this nice function update_option() . it is a nice way to store
WordPress spits posts in this format: <h2>Some header</h> <p>First paragraph of the post</p> <p>Second
WordPress has this thing that examines whether it has been set up (installed) on
My wordpress site has been hacked. Through this program: http://sitecheck.sucuri.net/ (...) i found that
For Wordpress TinyMCE when adding or editing posts. I have this issue where I
In WordPress I am using this code in my functions echo <script type=\text/javascript\> which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.