Steve Kim
Steve Kim

Reputation: 5591

Adding php parameters

So, here is original function which does not have any parameters (wordpress)

public static function getCurrent() {
    $post = get_post();
    $profiles = new MY_images($post->ID);
    return $profiles;
}

which is used in the following variable:

$my_site_images = MY_images::getCurrent();

So, it gets the $post->ID from the getCurrent()

Now, I want to customize it so that I can add any id in it or leave it empty for default function such as following:

$my_site_images = MY_images::getCurrent(343);  (where the "post id" is 343)

What modification do I need to make to the original function in order for me to add any ID in it??

Thanks bunch!

Upvotes: 0

Views: 47

Answers (1)

Joseph
Joseph

Reputation: 1086

You can choose to pass it a post id or leave it blank to get the current post id.

public static function getCurrent($post_id=false) {
    if($post_id !== false) {
        $profiles = new MY_images($post_id);
    } else {
        $post = get_post();
        $profiles = new MY_images($post->ID);
    }
    return $profiles;
}

Upvotes: 3

Related Questions