Anuj kumar Anuj kumar - 3 years ago 111
PHP Question

How to store the_title() values in array

i want to store the_title() values into an array so i can print them index wise where i want.

<?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' =>
'wpb_post_views_count' ,'orderby' => 'meta_value_num', 'post_type'=> 'idea'
) );
while ( $popularpost->have_posts()) : $popularpost->the_post();the_title();
endwhile;
?>


i tried one solution from stackoverflow but it can only store current post value in variable.

Answer Source

the_title() function prints the title or returns as string.

Use it third parameter to retrieve value instead of directly printing it.

Append it to pre defined array.

Thats it!!!

Parameters:

$before (string) (Optional) Markup to prepend to the title. Default value: ''

$after (string) (Optional) Markup to append to the title. Default value: ''

$echo (bool) (Optional) Whether to echo or return the title. Default true for echo. Default value: true

<?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' =>
        'wpb_post_views_count' ,'orderby' => 'meta_value_num', 'post_type'=> 'idea'
) );
$titles = array();
while ( $popularpost->have_posts()) : $popularpost->the_post();
$titles[] = the_title('', '', FALSE); // set third parameter to FALSE.
endwhile;

var_dump($titles);
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download