简体   繁体   中英

How to get first or second,.. recent post specifically in wordpress theme and put them in an array for use in javascript

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM