0

我正在尝试将 PHP 注入到 wordpress 博客页面上的 div 元素中。我让它可以处理静态内容,但我的脚本不会加载 PHP。我的动态/PHP代码:

<?php // data.php
    $args = array( 'numberposts' => 1, 'category' => 4, 'orderby' => 'rand' );
    $postslist = get_posts( $args );
    // Get posts associated with category 4
     foreach ($postslist as $post) :  setup_postdata($post); ?> 
          <h2><?php the_title(); ?></h2>
              <?php 
                  global $more; 
                   $more=0;
                   the_content('read more');
               ?> 
 <?php endforeach; ?>

我的ajax调用:

(function($) {
  $("div#content-here").click(function(){
  var url = '/wp-content/themes/elevenchild/data.php';

  $("div#content-here").load(url);
});
})( jQuery );   

我的观点自然有

<div id="content-here"></div>

元素。

我的 jquery 库加载正常。我可以注入简单的 HTML,所以我的方法有效,但它不适用于 PHP?在控制台中,它报告 500 Internal Server Error。

帮助

4

1 回答 1

0

不会那样工作。您在页面中“注入”了一些 PHP 代码,那又如何?那是因为您只在已经呈现的页面上完成了该操作,即您的客户端(浏览器)已经从服务器接收到的页面。它是动态创建的还是静态的 HTML 页面甚至都没有关系,因为您在浏览器中看到的是“最终产品”——静态 HTML。

回到我们的位置 - 您在 HTML 中插入了一些 PHP 代码。您现在需要的是运行该代码的服务器。一种方法是将其发送回服务器,但这不是工作方式。服务器将只执行 PHP 代码......在其管辖范围内,比方说(文档根等)。方法是将您的 PHP 代码注入服务器文件中,但如果有可能,那么整个网站都会受到威胁,幸运的是,这种情况并不经常发生。

于 2012-10-07T14:10:42.347 回答