【问题标题】:Ho do I add/require a full php file into a variable in a function我如何将完整的 php 文件添加/需要到函数中的变量中
【发布时间】:2013-03-21 07:28:02
【问题描述】:

我正在 Wordpress 中构建,并且有一些使用 jQuery.load 加载的内容。

$item = '<h2>'. the_title() .'</h2><div>'. the_content() .'</div>';
//$item = include 'myrequire.php'; This doesn't seem to work. 

我想知道如何让 php 变量 $item 加载一个 php 文件 - 这样我就可以保持 functions.php 整洁,并且可以像这样编写文件:

<h2><?php the_title(); ?><h2> 

...这有意义吗?

谢谢!

我遵循了这个教程: http://tomsbigbox.com/wordpress-load-more-posts-on-page-scroll/

这是完整的 sn-p:

function getArchives($count,$offset){
query_posts('posts_per_page='.$count.'&offset='.$offset);

$posts = array();

if(have_posts()) : while(have_posts()) : the_post();    


    $item = '<h2>'. the_title() .'</h2><div>'. the_content() .'</div>';
    //$item = include 'myrequire.php';

array_push($posts,$item); endwhile; endif;

return $posts;
};

【问题讨论】:

    标签: php wordpress function variables require


    【解决方案1】:

    http://www.php.net/manual/en/function.file-get-contents.php

    你想知道一个php文件的内容吗?

    【讨论】:

      【解决方案2】:

      include 执行文件,你想要file_get_contents

      $var=file_get_contents($filename);
      

      您也可以使用 get_template_part 以 wordpress 方式执行此操作,但这取决于您想要做什么。我会看一下法典http://codex.wordpress.org/Function_Reference/get_template_part

      【讨论】:

      • 谢谢,这看起来应该是正确的,但不幸的是我的代码无法加载文件。还有其他事情正在发生(它似乎忽略了我的工作示例中的 html 标签......和如果我抓取外部文件,它会加载 :/)
      • 可能是路径问题。在这种情况下,请尝试文件的绝对路径。或者,这可能是权限问题,在这种情况下尝试 chmod 777 该文件
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多