【问题标题】:PHP loading orderPHP 加载顺序
【发布时间】:2014-02-03 20:27:26
【问题描述】:

如何在“includes/part2”之后加载“includes/part1”?

这是一个示例代码:

<html>
<head>
</head>
<body>
    <h1>Txt</h1>
    <div>
        <?php get_template_part('includes/part1'); ?>
    </div>
    <h1>Txt</h1>
    <div>
        <?php get_template_part('includes/part2'); ?>
    </div>
</body>
</html>

这就是我的页面上的内容。 我希望浏览器先加载“includes/part2”,然后再加载“includes/part1”。

【问题讨论】:

  • 你能详细说明你的问题吗?
  • 您需要将该行放在代码的底部。 PHP 不是很异步的语言。
  • 加载整个页面后可以使用ajax加载此内容

标签: javascript php jquery wordpress


【解决方案1】:

在没有任何上下文的情况下同意,很难为您提供帮助。

如果 Bas Kuis 方法不是您需要的,那么也许这是

您可以使用 ob_start() 将其设置在缓冲区中,然后设置它,然后使用 $foo = ob_get_contents() 将其设置为变量。最后使用 echo $foo 回显结果。

ob_start();

get_template_part('includes/part');

$foo = ob_get_contents();

ob_clean();

echo $foo;

这里是一个使用 php 缓冲区的例子

PHP ob_start

但是我不太确定你会从中获得什么。

【讨论】:

  • 这不会起到相反的作用吗?
  • 这将在内容发送到浏览器之前执行'includes/part' php脚本。
【解决方案2】:

您可以简单地将其包含在页面底部:

<html>
    <head>
    </head>
    <body>
        <h1>Page</h1>
        <p>Stuff</p>
    </body>
</html>
<?php get_template_part('includes/part'); ?>

或者..

<html>
    <head>
    </head>
    <body>
        <h1>Page</h1>
        <p>Stuff</p>
        <div><?php get_template_part('includes/part'); ?></div>
    </body>
</html>

如果这更相关。

如果您需要在 dom 完全加载并准备好操作后运行某些东西,您可以:

<html>
    <head>
        <script src="path/to/jquery.js"></script>
        <script type="text/javascript">
            $().ready(function(){
                $.get("includes/part");
            });
        </script>
    </head>
    <body>
        <h1>Page</h1>
        <p>Stuff</p>
    </body>
</html>

无论哪种方式 - 您都必须提供更多关于您正在尝试做的事情的背景信息。

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 2010-09-06
    • 1970-01-01
    • 2012-11-02
    • 2016-09-27
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多