为此,将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]='';
我的需要是
  • 获取最新/流行的5篇文章标题及其永久链接
  • 然后将其分配给javascript变量,如上述代码所示或更好

    为什么我需要这个 我正在创建一个简单实用的新闻网站wordpress模板。我使用了一个javascript代码(来自web),它将显示我放入特定数组变量中的任何文本,比如滚动文本(以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.';
        ...
    
    。。。
    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;
    }
    });
    
    控制台日志将显示您可以访问哪些数据。以下是一个屏幕截图: