1

I have the following function where I would like to replace the image nopic.jpg

I have updated by deleting some redundant options.

    if ( ! function_exists( 'TaskerDev_get_first_post_image_fnc' ) ) :
            function TaskerDev_get_first_post_image_fnc($pid, $w = 100, $h = 100) {

                //---------------------
                // build the exclude list
                $exclude = array();

                $args = array(
                'order'          => 'ASC',
                'post_type'      => 'attachment',
                'post_parent'    => $pid,
                'meta_key'       => 'another_reserved1',
                'meta_value'     => '1',
                'numberposts'    => -1,
                'post_status'    => null,
                );
                $attachments = get_posts($args);
                if ($attachments) {
                        foreach ($attachments as $attachment) {
                        $url = $attachment->ID;
                        array_push($exclude, $url);
                    }
                }

                //-----------------

                $args = array(
                'order'          => 'ASC',
                'orderby'        => 'post_date',
                'post_type'      => 'attachment',
                'post_parent'    => $pid,
                'exclude'           => $exclude,
                'post_mime_type' => 'image',
                'post_status'    => null,
                'numberposts'    => 1,
                );
                $attachments = get_posts($args);
                if ($attachments) {
                    foreach ($attachments as $attachment) {
                        //$url = wp_get_attachment_url($attachment->ID);
                        return TaskerDev_wp_get_attachment_image($attachment->ID, 'thumb_image_thing');
                    }
                }
                #else   return get_bloginfo('template_url').'/images/nopic.jpg';
                else    return get_stylesheet_directory_uri().'/images/Picture16.jpg';
            }
            /*************************************************************
            //*
            *   TaskerDev (c) sitemile.com - function
            /
            *************************************************************/
        endif;

When done, everything works fine for about an hour and then I get the following error:

the function TaskerDev_get_first_post_image_fnc() cannot be called twice, it's been called in the parent already (just paragraphing what it says)

What is the most efficient way to update the image by been able to called it within the child theme. IS there a short hook or action maybe? Thanks.

  • 1
    You want to have look at this – Pieter Goosen Jun 7 '15 at 8:57
  • What happens if you remove this if ( ! function_exists( 'TaskerDev_get_first_post_image_fnc' ) ) : and thisendif; – Brad Dalton Jun 7 '15 at 9:06
  • It works, but maybe this is what I might not mentioned before. I see the change immediately, but after 20 to 30 minutes I get the fatal error about cannot redeclared such function that is already in parent theme. That why I was using if (!function_exists('')) – cmsdeployed Jun 7 '15 at 9:16
-1

You should wrap your function in parent theme like this, not in child theme function.php

if (!function_exists('TaskerDev_get_first_post_image_fnc') ){
function TaskerDev_get_first_post_image_fnc($pid, $w = 100, $h = 100)
    {
//Parent theme code here
 }
}
| improve this answer | |
  • but I will lose the the customization after an upgrade of the parent theme! Unless I don't understand what you mean. – cmsdeployed Jun 7 '15 at 8:47
  • I have been getting fatal error: cannot rdeclare TaskerDev_get_first_post_image() (prviously declared in ....child theme functions.php...) in url of parent/functions.php. Is it the whole point of child theme preserving customization in case of parent upgrade? – cmsdeployed Jun 7 '15 at 8:50
  • you can not overwrite the functions in child theme unless they are wrapped in if function_exists condition within parent theme. If the parent theme developer has not wrapped your required function, you have no choice left to either do what I said or try to find filter/hook or action that triggers this function and then deregister that and re-register it again with your custom function. – Wali Hassan Jun 7 '15 at 9:46
  • yes you right, I have contacting the theme developer and hope to get a filter/hook, but I doubt it. – cmsdeployed Jun 7 '15 at 9:55

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.