javascript - WordPress 在 jquery 中发布 while 循环

标签 javascript php jquery wordpress

我想在 javascript 中循环所有帖子标题来构建我的画廊,但它不起作用,我得到奇怪的输出 我的代码:

  <?php
        query_posts(array( 
            'post_type' => 'musings'
        ) );  
         while (have_posts()) : the_post(); 
        $title = the_title();
        $desc = the_content();
        $galleryslider .= "{
                        'title'         : '".$title."',
                        'description'   : '".$desc."',
                        'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                        'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                        'button_list'   :
                        [
                            { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                            { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                        ],
                        'tags'          : ['Portrait']
                    },";
     endwhile;
     $galleryslider = rtrim($galleryslider,',');
    ?>
    <script type="text/javascript">
    $(function(){
        <?php echo $finalslider;?>
    });
    </script>

预期输出

{
                'title'         : 'post 1 title',
                'description'   : 'post 1 desc',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            },{
                'title'         : 'post 2 title',
                'description'   : 'post 2 desc',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            }

我得到的结果(wordress页面在javascript上方显示标题和desc,但javascrit内标题和desc的值是空白,请参见下面的代码)

{
                'title'         : '',
                'description'   : '',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            },{
                'title'         : '',
                'description'   : '',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            }

最佳答案

根据我对 WordPress 的了解,尝试直接回显数据或保存到输出缓冲区并稍后回显。此外,您可能必须在 the_title() 上返回 false 才能使用 strip_tags()。另外,$desc 似乎没有定义。:

<?php
    ob_start();
    query_posts(array('post_type' => 'musing'));

    while (have_posts()) : the_post(); ?>
        {
            // the_title() can use a false to return the value for use with strip_tags (apparently)
            'title'         : '<?php echo stripslashes(the_title(false)); ?>',
            // Where is this $desc coming from?
            'description'   : '<?php echo $desc; ?>',
            'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
            'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
            'button_list'   : [
                { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
            ],
            'tags'          : ['Portrait']
        }, <?php
    endwhile;
    $data   =   ob_get_contents();
    ob_end_clean();
?>
<script type="text/javascript">
    $(function(){
            <?php echo $data; ?>
        });
</script>

关于javascript - WordPress 在 jquery 中发布 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573894/

相关文章:

javascript - 发现 if 语句中的特定参数不正确?

javascript - 在 Ajax 中接收 Div 的数据

PHP 语法、数组和错误

javascript - 获取 html5 Canvas 上每个绘制矩形的特定颜色

javascript - jQuery SlideDown 脚本在简单代码示例中不起作用

javascript - 在 forEach 中调用 firebase 会使迭代不起作用

javascript - Twig-Template 中的数据表中的静态子行(附加信息)

javascript - React.js onChange={value => {}} 多个值

javascript - 在页面加载时运行 JS 函数

jquery - 单击评论按钮时如何显示每个容器的评论框?