In functions.php I have made the following code:
add_action( 'listofnames', 'SomeNames' );
function SomeNames(){
$names=array( "john","edgar","miles");
return $names;
}
It should return array I wish to manipulate with, but when in index.php I try, it goes wrong. Obviously it does not work this way:
do_action('listofnames');
foreach (do_action('listofnames') as $n){
echo $n;
}
Can anybody help me to manage this?
In functions.php I have made the following code:
add_action( 'listofnames', 'SomeNames' );
function SomeNames(){
$names=array( "john","edgar","miles");
return $names;
}
It should return array I wish to manipulate with, but when in index.php I try, it goes wrong. Obviously it does not work this way:
do_action('listofnames');
foreach (do_action('listofnames') as $n){
echo $n;
}
Can anybody help me to manage this?
Share Improve this question asked Mar 2, 2020 at 1:14 Bunny DavisBunny Davis 1133 bronze badges1 Answer
Reset to default 2A callback for an action doesn't return anything, because that return value is never passed through by WordPress.
Use a filter instead:
add_filter( 'listofnames', 'SomeNames' );
function SomeNames() {
$names=array( "john","edgar","miles");
return $names;
}
And in your template you call it like this:
$names = apply_filters( 'listofnames', [] );
foreach ( $names a $name ) {
echo $name;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744703475a4588912.html
评论列表(0条)