我为我正在为客户开发的 WordPress 主题创建了一个简单的 javascript 幻灯片数组。该数组如下所示:
var bca = [
'',
'<h2>Heading Number 2</h2><p>Content for section 2</p>',
'<h2>Heading Number 3</h2><p>Content for section 3</p>',
'<h2>Heading Number 4</h2><p>Content for section 4</p>'
];
在数组的第一部分中,我想显示可以使用以下代码显示的最新 WordPress 博客文章:
<?php
$recent_posts_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3));
while ($recent_posts_query->have_posts())
{$recent_posts_query->the_post();
?>
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<h2>
<a class="frontpage-posth2" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
<a href="<?php the_permalink(); ?>" class="blog-btn">READ MORE...</a>
<div class="clearfix"></div>
<?php
}
?>
当然,我不能只是将其粘贴到数组中,而是想知道是否有人有任何想法或任何相关教程可以让我将其添加到上面的数组中! 非常感谢
- 菲利普·杜斯
最佳答案
您可以在 javascript 中拥有一个在 php 中动态生成的数组。
<?php
$array = ['apple', 'banana', 'orange'];
$arrayPhp = json_encode($array);
echo "<script type='text/javascript'>
var arrayJs = $arrayPhp;
console.log(arrayJs);
console.log(arrayJs[0]);
</script>"
?>
关于javascript - 简单JavaScript数组innerHtml中的php代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37924363/