1

Here are two example PHP functions:

function myimage1() {
global $post;
    $attachment_id = get_post_meta($post->ID, 'f1image', $single = true);
    $myimage1 = wp_get_attachment_image( $attachment_id, thumbnail );
return $myimage1;
}

(I call this one like this: <?php echo myimage1(); ?> )

and

function myimage2() {
global $post;
    $attachment_id = get_post_meta($post->ID, 'f1image', $single = true);
    $myimage2 = wp_get_attachment_image( $attachment_id, medium );
return $myimage2;
}

(I call this one like this: <?php echo myimage2(); ?> )

As you can see, both have one common variable $attachment_id. And both functions are very much related, and it's just that I don't know how to combine them both, or how to call them from the combined function.

PS: I don't know PHP, my terminology could be a bit vague. In that case, please feel free to correct me.

2 Answers 2

2
function myimage($level) {
    global $post;
    $attachment_id = get_post_meta($post->ID, 'f1image', true);
    $myimage = wp_get_attachment_image( $attachment_id, $level );
    return $myimage;
}

myimage("medium");
myimage("thumbnail");
Sign up to request clarification or add additional context in comments.

Comments

0

OOP is exactly for this needs. Two functions (methods) uses the same class variable.

<?php

class A {

    public $attachment_id;

    private function wp_get_attachment_image() {

        // place your wp_get_attachment_image() function content here

    }

    public function myimage1() {

        $myimage1 = $this->wp_get_attachment_image($this->attachment_id, medium);
        return $myimage1;

    }

    public function myimage2() {

        $myimage2 = $this->wp_get_attachment_image($this->attachment_id, medium);
        return $myimage2;

    }

}

$a = new A;
$a->attachment_id = $a->get_post_meta($post->ID, 'f1image', $single = true);
$a->myimage1();
$a->myimage2();

?>

1 Comment

This is really confusing to me. Could you please modify your example with the code in my question? Also, please provide the PHP code to call each PHP function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.