为此,将Wordpress post信息分配给PHP数组,并将PHP数组值分配给javascript数组 使用Wordpress中的值将PHP转换为Javascript
我希望下面的代码解释了我想要的为此,将Wordpress post信息分配给PHP数组,并将PHP数组值分配给javascript数组 使用Wordpress中的值将PHP转换为Javascript,javascript,php,html,wordpress,content-management-system,Javascript,Php,Html,Wordpress,Content Management System,我希望下面的代码解释了我想要的 <?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 websit
<?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>
var list=新数组();
列表[0]='';
列表[1]='';
列表[2]='';
列表[3]='';
列表[4]='';
我的需要是
...
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.';
...
。。。
var list=新数组();
列表[0]='
看看快讯部分,这就是我所说的
我从中获得了完整的javascript代码
简而言之,我期望的输出是
以滚动文本样式显示最新的5或10篇博客文章,无需手动更新
[对于我这边的错误沟通,我深表歉意。希望你们理解我的问题。]
谢谢。:) 只需对数组进行json_编码即可。以下是一个例子:
首先你得到你的帖子:
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
然后在脚本标记中对其进行json_编码
<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>
jQuery(函数(){
var postArray=;
控制台日志(postArray);
对于(邮资中的e){
var postInfo=postArray[e];
控制台日志(postInfo);
//如何获取标题:
var postTitle=postInfo.post\u title;
}
});
控制台日志将显示您可以访问哪些数据。以下是一个屏幕截图: