Why different works get_posts() in the body page and in function.php?

Prescribed in the body of the page code:
<?php
 $posts = get_posts();
 foreach( $posts as $post ){
setup_postdata($post);
 // the format of the output the_title() ...
 echo '<br/>';
the_title();
}

 wp_reset_postdata(); // reset
?>

Displays the title of the posts as it should:
21312213
asdas2323 2323
asdasdsad2323
asdfsadasdf
zapis k rubrike 2
And use in page your action prescribed in function.php:
PAGE:

<?php do_action('ohu_action'); ?>

FUNCTION.PHP:

add_action('ohu_action', 'ohu_func');
ohu_func function(){
// default settings
$posts = get_posts();
foreach( $posts as $post ){
setup_postdata($post);
 // the format of the output the_title() ...
 echo '<br/>';
the_title();
}

wp_reset_postdata(); // reset
}

Displays the name of the posts:
asdasdsad2323
asdasdsad2323
asdasdsad2323
asdasdsad2323
asdasdsad2323
March 20th 20 at 11:46
0 answer

Find more questions by tags PHPWordPress