Playing around with a plugin for Wordpress. I'm trying to figure out how to pass multiple values into a function and then use those values within the function.
For example:
<?php myfunction(1,2,3); ?>
So if someone uses that template tag and passes in the values of 1, 2, and 3, I want to grab all those values they pass in and do something with them in the function I'm writing.
How would I go about doing that? I believe I need a loop to to store each value into an array right?
Thanks Bob. I think that's fine if we know only three values are being passed in. If I don't know how many values will be passed in, how should I approach it?
For example, if the values being passed in are category ids like 1,2,3,4, I want to take those values and plug them into a function that uses exclude=1,2,3,4.
I'm not much of a WordPress person but it seems many of the arguments are passed name/value pairs as string, so you'd need to build some of those strings.
For example:
So if someone uses that template tag and passes in the values of 1, 2, and 3, I want to grab all those values they pass in and do something with them in the function I'm writing.
How would I go about doing that? I believe I need a loop to to store each value into an array right?
The following code would be the code for your function:
That adds them up and will output the total, if the function is used.
To use the function, you can use the code you posted above, and it will output 6. Hope this helps and is what you mean.
For example, if the values being passed in are category ids like 1,2,3,4, I want to take those values and plug them into a function that uses exclude=1,2,3,4.
So if someone used
<?php myfunction(array(1,2,3)) ?>, then I wanted the values of 1,2,3 to be passed intoget_categories('exclude=1,2,3')You could use the func_get_args() function which is used like:
Another way is restricting to associative array, so you have named values.
I'm not much of a WordPress person but it seems many of the arguments are passed name/value pairs as string, so you'd need to build some of those strings.
I have no idea if I helped here.
<?php myfunction('exclude=1,2,3') ?>