执行php(wordpress调用),在页面加载后生成html

时间:2013-10-11 16:39:05

标签: php html wordpress onload

我的网站上的子目录中有一个wordpress博客,并在我的主页上显示了一些最新的帖子。为此,请使用以下代码

<?php
// Include WordPress 
define('WP_USE_THEMES', false);
require('blog/wp-load.php');
query_posts('showposts=1');
?>
<?php
require('blog/wp-blog-header.php');
?>
<?php query_posts('showposts=4'); ?>
<?php while (have_posts()) : the_post(); ?>
<h5><?php the_title(); ?></h5>
<p><a href="<?php the_permalink(); ?>"><?php the_excerpt(); ?></a></p>
<?php endwhile;?>

我一直试图弄清楚它是否可能在页面加载后执行此代码,因为这会减慢我的页面速度。 我已经尝试使用body onLoad函数,它正确执行代码,但生成的html不显示

<body onLoad="init();">

<script language="javascript">
function init()
{
ABOVE CODE IN HERE
}
</script>
</body>

任何人都可以告诉我该怎么做

1 个答案:

答案 0 :(得分:0)

最好的方法是通过AJAX。

创建页面sidebar_render.php。把那个php代码放在一个文件中,比如sidebar_render.php。 在页面中插入这些标记以包含jquery,然后获取HTML并将其放在某处。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
    $(document).ready(function(){
      $.ajax({
          url: '/sidebar_render.php',
         data: {},
         success: function(data){
              $('#your_wp_sidebar').html(data);
              },
         dataType: 'html'
       });
</script>

这将检索sidebar_render.php HTML,然后使其显示在您指定的ID的标记中。因此,您需要在“成功”功能中填写正确的位置。

请参阅http://api.jquery.com/jQuery.ajax/

相关问题