gpt4 book ai didi

javascript - WordPress 在 jquery 中发布 while 循环

转载 作者:行者123 更新时间:2023-12-03 10:22:15 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com