【问题标题】:Wordpress doesnt proces external php fileWordpress 不处理外部 php 文件
【发布时间】:2015-06-25 13:32:26
【问题描述】:

我正在尝试为 wordpress 编写自己的主题,并编写了一个文件,其中包含我所有的模态(twitter bootstrap),它们是 html。我将它添加到主题部分(创建了一个 inc 文件夹)并将其命名为 modals.php。

我通过 php 将它包含在我需要模态的页面中的任何地方。这行得通。每当我单击链接时,都会加载模式。但是,当我开始将 php 添加到模式时,它会中断。我的模态正在加载,但不是从 wordpress 数据库中请求的帖子项目(我用它来使其成为动态内容,因为它是多语言的),它加载了页脚,并破坏了我的网站,这真的让我感到困惑。

我的 modals.php 包含以下内容

<div id="about" class="modal fade" tabindex="-1">
    <div class="vertical-alignment-helper">
        <div class="modal-dialog vertical-align-center">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" data-dismiss="modal"></button>
                    <button class="close" type="button" data-dismiss="modal">x<span class="sr-only">Close</span></button>&nbsp;
                    <h4 id="myModalLabel" class="modal-title">
                        My modal popup
                    </h4>
                </div>
                <div class="modal-body modal-list">
                    <div class="modal-content col-md-12">
                        <strong><?php _e(" <!--:en-->It's me<!--:--><!--:pl-->To Ja<!--:-->" ); ?> </strong>
                            <p>
                                <?php
                                    $post = get_post(930); 
                                    $content = $post->post_content;
                                    echo $content;
                                ?>  
                            </p>
                    </div>
                </div>
                <div class="modal-footer"><button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</div>

我是否需要告诉我的文件它需要读取特定于 wordpress 的文件,或者我是否需要将我的文件添加到某处才能使其读取?

编辑

当我尝试只是回显诸如 test 之类的单词时,这是可行的,所以我感觉它与 Wordpress 使用的语法有关。有没有办法让 wordpress 知道它应该用 wordpress 语法阅读?

编辑 2

目前,我有多个子页面,所以我只想为某个页面加载某些模式。对于 gdynia,我只想加载那些。我更新了代码,现在有了。

else if (is_page ('gdynia')) {
        get_template_part('modals-gdynia.php'); 
}

然而现在什么都没有发生。以前是,

else if (is_page ('gdynia')) {
    include_once(get_template_directory_uri() .'/modals-gdynia.php');
}

【问题讨论】:

  • 蛞蝓匹配吗? gdynia。此外,如果您只是为了它而在默认主页上测试它is_front_page()。另外你在哪里做这个条件?它是在模板文件中还是在functions.php 文件中?

标签: php wordpress


【解决方案1】:

如果您使用get_template_part(),您应该没有任何问题。

出于某种原因,您可能包含 modals.php 的方式可能是调用 wp_footer()

所以如果你想使用这个模板,你只需要在你想要的相应文件中调用它。

header.php single.php footer.php

是这样的模板默认文件。 在其中一个中调用它。

<?php 
    get_template_part('modals.php'); 

    // Just in case you wanna use a variable from another context into this template. 
    $post_id = 930;
    include(locate_template('modals.php'));
?>

除此之外,我认为它们不应该是它不起作用的问题。

如果它解决了您的问题,请告诉我,也许将代码上下文粘贴到调用 modals.php 的位置以及调用方式。

【讨论】:

  • 我不知道模板部分。我刚刚在页脚中添加了包含作为 `include_once(get_template_directory_uri() .'/inc/modals-gdynia.php'); `。我实施了您的建议,但没有任何效果。我将 modals-gdynia.php 文件移动到主题文件夹,因此子文件夹不需要扩展名,但到目前为止还没有运气。我会更新我的帖子
猜你喜欢
  • 1970-01-01
  • 2016-03-21
  • 2020-05-23
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
相关资源
最近更新 更多