[英]Assigning Wordpress post information to PHP array and assign the php array value to javascript array FOR THIS REASON
我希望以下代碼可以解釋我想要的內容。
<?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創建一個簡單且有效的新聞網站wordpress模板。 而且,我使用了一個javascript代碼(從網絡上獲取),將顯示放置在特定數組變量中的所有文本,例如滾動文本(以Flash新聞/最新新聞樣式顯示)。
現在,我希望使用最新的博客/新聞帖子動態更新滾動文本,而不是像現在這樣是靜態的。
...
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.';
...
當前在此地址上臨時提供iam創建的網站
查看Flash新聞部分-這就是iam所說的。
我從獲得完整的javascript代碼 http://javascripts.vbarsan.com/
以滾動文本樣式顯示最新的5或10個博客文章,而無需手動更新。
[對不起,我這邊的通訊有誤。 希望大家理解我的問題。 ]
謝謝。 :)
只需json_encode數組。 這是一個例子:
首先,您將收到您的帖子:
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
然后,您可以在腳本標簽中對其進行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>
控制台日志將顯示您可以訪問的數據。 這是屏幕截圖:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.