I hope the following code explains what i want.
<?php
$title = array();
$i=0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
$title[i]=the_title();
$link[i]=the_permalink();
$i++;
endwhile; else:
$title[0]="Welcome to my website.";
$link[0]="/index.php";
endif;
?>
<script>
var list=new Array();
list[0]='<a href="<?php echo $link[0] ?>"><?php echo $title[0] ?></a>';
list[1]='<a href="<?php echo $link[1] ?>"><?php echo $title[1] ?></a>';
list[2]='<a href="<?php echo $link[2] ?>"><?php echo $title[2] ?></a>';
list[3]='<a href="<?php echo $link[3] ?>"><?php echo $title[3] ?></a>';
list[4]='<a href="<?php echo $link[4] ?>"><?php echo $title[4] ?></a>';
</script>
Iam creating a simple & working news website wordpress template. And I used a javascript code(got from the web) that will display any text i put inside a specific array variable like a scrolling text( in a flash news/breaking news style).
Now I want the scrolling text to be dynamically updated with the latest blog/news post instead being static like now.
...
var list=new Array();
list[0]='<a href="This is manually typed news one.';
list[1]='<a href="This is manually typed news two.';
list[2]='This is manually typed news three.';
list[3]='This is manually typed news four.';
list[4]='This is manually typed news five.';
...
The website iam creating currently is temporarily available on this address
Look at the section - that is what iam talking about. 部分-这就是iam所说的。
I got the from http://javascripts.vbarsan.com/
To display the latest 5 or 10 blog posts in a scrolling text style without manually updating.
[Sorry for any wrong communication on my side. Hope you people understand my question. ]
Thanks. :)
Just json_encode the array. Here is an example:
First you get your posts:
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
Then you json_encode it in a script tag.
<script type="text/javascript">
jQuery(function(){
var postArray = <?php echo json_encode($posts_array); ?>;
console.log(postArray);
for (e in postArray) {
var postInfo = postArray[e];
console.log(postInfo);
//how to get the title:
var postTitle = postInfo.post_title;
}
});
</script>
The console log will show you which data you can access. Here is a screenshot:
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.