PHP to Javascript with values from Wordpress
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>
My need is to
获得最新/最受欢迎的5个帖子标题及其永久链接
然后将其分配给javascript变量,如上面的代码或更好
Why I need this
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).
我创建一个简单的工作新闻网站wordpress模板。我使用了一个javascript代码(来自网络),它将显示我放在特定数组变量中的任何文本,如滚动文本(以flash新闻/突发新闻样式)。
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.';
...
Reference
The website iam creating currently is temporarily available on this address
目前创建的网站暂时可在此地址上获取
Look at the Flash News section - that is what iam talking about.
看看Flash新闻部分 - 这就是我所说的。
I got the complete javascript code from http://javascripts.vbarsan.com/
我从http://javascripts.vbarsan.com/获得了完整的javascript代码
In short, The Output Iam expecting is
To display the latest 5 or 10 blog posts in a scrolling text style without manually updating.
以滚动文本样式显示最新的5或10篇博文,无需手动更新。
[Sorry for any wrong communication on my side. Hope you people understand my question. ]
[抱歉我身边有任何错误的沟通。希望你们的人理解我的问题。 ]
Thanks. :)
1 个解决方案
#1
Just json_encode the array. Here is an example:
只需json_encode数组。这是一个例子:
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.
然后你在脚本标签中对它进行json_encode。
<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:
控制台日志将显示您可以访问的数据。这是一个截图:
#1
Just json_encode the array. Here is an example:
只需json_encode数组。这是一个例子:
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.
然后你在脚本标签中对它进行json_encode。
<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:
控制台日志将显示您可以访问的数据。这是一个截图: