-1

Iam creating a theme where i need to work this process.

I tried in many ways and made a number of search/research but failed to find the way.

I asked a similar question before with full details which is at,

Assigning Wordpress post information to PHP array and assign the php array value to javascript array FOR THIS REASON

Please answer on any of the questions. Thank you.

Community
  • 1
  • 1
Saravanan
  • 23
  • 6
  • 1
    possible duplicate of [Assigning Wordpress post information to PHP array and assign the php array value to javascript array FOR THIS REASON](http://stackoverflow.com/questions/30046967/assigning-wordpress-post-information-to-php-array-and-assign-the-php-array-value) – whitfiea May 10 '15 at 11:02

1 Answers1

1

here is a way to get your posts in many different ways into an array:

<?php $args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => '',
    'category_name'    => '',
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);
$posts_array = get_posts( $args ); ?>

I think Descending order should make it go from the latest post but if not try Ascending.

As far as getting your php array into a javascript array it can be done with the push method like so:

<script type="text/javascript" language="javascript">
    var pausecontent = new Array();
    <?php foreach($posts_array as $key => $val){ ?>
        pausecontent.push('<?php echo $val; ?>');
    <?php } ?>
</script>

Anyway hope that helps, should work fine but let me know if you run into any complications

rnevius
  • 26,578
  • 10
  • 58
  • 86
Alex
  • 132
  • 1
  • 1
  • 9
  • :-( Thanks for answering. I tried this but iam not getting any improvements. and my doubt is can we use inside – Saravanan May 10 '15 at 11:01
  • Yeah defenitely, I believe it's just you can't send a javascript array into php. But I actually found that as an answer here, though can'n seem to find it now: here is another method: – Alex May 10 '15 at 11:09
  • oh here is where I found that original answer: http://stackoverflow.com/questions/12813580/how-to-assign-php-array-values-to-javascript-array – Alex May 10 '15 at 11:12
  • oh wait, the post array returns a multidimensional array, that's why it can't just go into a regular javascript array: https://codex.wordpress.org/Template_Tags/get_posts – Alex May 10 '15 at 11:21