分配Wordpress发布信息到PHP数组,并将php数组值分配给javascript数组为此原因 [英] Assigning Wordpress post information to PHP array and assign the php array value to javascript array FOR THIS REASON

查看:165
本文介绍了分配Wordpress发布信息到PHP数组,并将php数组值分配给javascript数组为此原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP to Javascript with values from Wordpress



我希望以下代码解释我想要的。

 <?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] =欢迎来到我的网站。
$ link [0] =/ index.php;

endif;

?>

< script>

var list = new Array();
list [0] ='< a href =<?php echo $ link [0]?>><?php echo $ title [0]?& ';
list [1] ='< a href =<?php echo $ link [1]?"><?php echo $ title [1]?& ';
list [2] ='< a href =<?php echo $ link [2]?>><?php echo $ title [2]?& ';
list [3] ='< a href =<?php echo $ link [3]?>><?php echo $ title [3]?& ';
list [4] ='< a href =<?php echo $ link [4]?"><?php echo $ title [4]?& ';

< / script>



我需要



  • 获取最新/热门5帖子标题及其固定链接

  • 然后将其分配给javascript变量,如上述代码或更好


  • 为什么我需要这个



    Iam创建一个简单的工作新闻网站wordpress模板。我使用了一个javascript代码(从网站获取),它会显示我放在一个特定的数组变量,如滚动文本(在新闻新闻/突发新闻风格)的任何文本。



    现在,我想使用最新的博客/新闻文章动态更新滚动文本,而不是像现在一样静态。

      ... 
    var list = new Array();
    list [0] ='< a href =这是手动输入的新闻二。';
    list [1] ='< a href =这是手动输入的新闻二。
    list [2] ='这是手动输入的新闻三。
    list [3] ='这是手动输入的新闻四。
    list [4] ='这是手动输入的新闻五。
    ...



    参考



    目前正在创建的网站暂时可以在此地址使用




    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

  • get the latest/popular 5 post title and its permalink
  • then assign it to the javascript variable like in the above code or better
  • 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).

    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

    www.iamone.in/todaynewstv.

    Look at the Flash News section - that is what iam talking about.

    I got the complete javascript code from http://javascripts.vbarsan.com/

    In short, The Output Iam expecting is

    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:

    这篇关于分配Wordpress发布信息到PHP数组,并将php数组值分配给javascript数组为此原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆