I'm working on a small function for a WordPress site, and can't figure why my function is echoing the data on my webpage.
My function is:
function get_post_titles($args) {
$titles = array();
$posts = new WP_Query($args);
while ($posts->have_posts()){
$posts->the_post();
$titles[] = the_title();
}
return $titles;
}
and I call it in a separate .php file:
<?php
$titles = get_post_titles($args);
?>
<select>
<option selected="selected">Choose an Article</option>
<?php
foreach($titles as $title){
?> <option> $title </option><?php //this isn't working, pls ignore for this post
}
?>
</select>
...
The result is a webpage with a Select box (yay!) but for some reason, the page also shows ALL of the titles in a row (it outputs the full array).
I would expect that if I had say, echo get_post_titles($args), but nowhere in my code do I ask it to echo. Why is my array visible?
I think it's because of my $titles = get_post... line. But again, why is it displaying $titles instead of simply declaring/assigning the variable?
(I know there's a WordPress SE site, but I think this is more directly a PHP issue than WP issue. However, if I should post over there instead, let me know).