get_the_content() 结果显示不正确 [英] result of get_the_content() not correctly displayed

查看:35
本文介绍了get_the_content() 结果显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a javascript function that creates an array and fill it with the title and content of Wordpress posts. In other words, I try to put the result of get_the_content() and get_the_title() in a javascrip array using the loop, and display them in a separate div. The problem is that the result of get_the_content() dont appear in the div. not like with get_the_excerpt() or get_the_title() which both of them are correctly stored in javascript variable and correctly displayed in the div after onclick event.

代码:

<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];   
            }

}

提前致谢

推荐答案

您正确地为标题添加了引号,但没有为内容添加引号.当您尝试结果时,几乎肯定会遇到 JavaScript 错误.

You're correctly adding quotes for the title, but not for the content. You're almost certainly getting JavaScript errors when you try the results.

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

You should probably also make sure that the content strings don't contain quotes, because that would also cause errors (and would constitute a possible XSS vector, depending on where the content comes from). The simplest way to do that is to use a JSON encoding facility.

这篇关于get_the_content() 结果显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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