get_the_content()的结果未正确显示

时间:2012-03-17 21:24:45

标签: php javascript wordpress

我有一个javascript函数,可以创建一个数组并用Wordpress帖子的标题和内容填充它。换句话说,我尝试使用循环将get_the_content()get_the_title()的结果放在javascrip数组中,并将它们显示在一个单独的div中。 问题是get_the_content()的结果不会出现在div中。与get_the_excerpt()或get_the_title()不同,它们都正确存储在javascript变量中,并在onclick事件后正确显示在div中。

代码:

<script type="text/javascript">

function initialize(str) {

<? echo 'var topics = [';?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>    

<?php $title=get_the_title();?>

<?php 
echo "['";
echo $title;
echo "',";
$content=get_the_content();
echo $content;
echo "],";  ?>

<?php endwhile; ?>
<?php endif; ?>

<?php echo '];'; ?>  

for (i = 0; i < topics.length; i++) 
{ 
if(topics[i][0]==str) {
document.getElementById("id").innerHTML = locationsmee[i][1];   
            }

}

提前致谢

2 个答案:

答案 0 :(得分:1)

您正确地为标题添加引号,但不为内容添加引号。您在尝试搜索结果时几乎肯定会收到JavaScript错误。

您还应该确保内容字符串不包含引号,因为这也会导致错误(并且会构成可能的XSS向量,具体取决于内容的来源)。最简单的方法是使用JSON编码工具。

答案 1 :(得分:0)

我找到了基于Pointy答案的完整解决方案。要将wordpress的数据编码为JSON,可以通过以下两种代码之一来完成:

<?php
header('Content-Type: text/html; charset: UTF-8');
require( '../English/The-Blog/wp-load.php' );

query_posts(array('posts_per_page' => 20,));

$jsonpost = array();
$i=0;
if (have_posts()) :
while (have_posts()) : the_post(); 


  $jsonpost[$i]["id"] = get_the_ID();
  $jsonpost[$i]["title"] = get_the_title();
  $jsonpost[$i]["url"] = apply_filters('the_permalink', get_permalink());
  $jsonpost[$i]["content"] = apply_filters('the_content', get_the_content());


  $jsonpost[$i]["date"] = get_the_time('d F Y');
  $i=$i+1;
endwhile;
endif;

header('Content-type: application/json;');
echo json_encode($jsonpost);
?>

<强> OR

<?php


define('WP_USE_THEMES', false);
require('../English/The-Blog/wp-blog-header.php');

$posts = get_posts(array(
        'numberposts' => 5,
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
        'post_status' => 'publish'
        ));
$json = array();


if ($posts) {
    foreach ($posts as $post) {
        $ray = array();
        the_post();
        $ray['id'] = $post->ID;
        $ray['date'] = $post->post_date;
        $ray['contents'] = $post->post_content;
        $ray['title'] = $post->post_title;
        $json['posts'][] = $ray;

    }
}
header('Content-type: application/json;');
echo json_encode($json);
?>

这两个代码都提供了一个JSON字符串,可以通过jQuery访问/显示,如下所示:

<script>
jQuery(document).ready(function($){
    $(".load").click(function(){

        $.getJSON(
            'phpscript.php',
            function(data){
                      $('#9lessonsLinks').hide();
                     for (var i=0 ; i < data.length ; i++)
            {
                    var personne = data[i];

         var div_data ="<div class='box'><a>"+personne.url+"</a></div>";

        $(div_data).appendTo("#9lessonsLinks");


                          }



            $('#9lessonsLinks').fadeIn();

            }
        );
    });
});

</script>