Working with wordpress function.
I have to make a function which is already made with static info, to be dynamic. I am creating a custom field so that user can insert that info.
I need to pass an array of links associated with a name, which would be perhaps some post name. The current code has the following structure:
function my_function() {
$First_link = 'http://www.myurl.com.br';
$Second_link = 'http://www.mysecondurl.com.br';
...etc
$allthose_links = array(
$First_link => 'First Quiz',
$Second_link => 'Second Quiz',
...etc
);
return $allthose_links;
}
I need this to be something like:
function my_function() {
$posts = get_posts($args);
$allthose_links[] = array();
foreach( $posts as $post ) {
$thelink = get_field( $post=>['mylink']);
$thename = $post->name;
}
$allthose_links = array( //here's the deal
$thelink => $thename
)
return $allthose_links;
}
CodePudding user response:
The $allthose_link
array should be inside the foreach loop so that each of the links will be added to $allthose_link
array on every iteration using the link as the key. Your new function should look like the example below:
function my_function() {
$posts = get_posts($args);
$allthose_links = array();
foreach( $posts as $post ) {
$thelink = get_field( $post=>['mylink']);
$thename = $post->name;
$allthose_links[$thelink] = $thename;
}
return $allthose_links;
}