为什么我无法访问 Wordpress 单个自定义帖子类型模板中的$post数据?



我在Wordpress插件开发中遇到了一个非常奇怪的问题:

我做了一个自定义的帖子类型谁的工作完美。我实际上必须在插件上定义我的single.php模板来提出一个现成的公共页面。

我找到了single_template"过滤器,它允许我定义自己的模板,并为我的自定义帖子类型覆盖默认模板。

它实际上运行良好,我可以开始集成了。

我的问题是,我不能访问POST对象/数据在这个模板中,我没有发现很多类似的情况在网络上,将感谢任何帮助来解决这个问题。

下面是我的部分代码:

过滤器为单个页面定义我自己的模板

function get_teams_single_template( $single_template ) {
    global $post;
    if ( $post->post_type == 'mt_teams' ) {
        $single_template = cdev_mt_directory . 'templates/teams/single-mt_teams.php';
    }
    return $single_template;
}
add_filter( 'single_template', 'get_teams_single_template' );

我试图得到我的方式=>$post return null

<?php     
    include_once '../../../../../wp-load.php';
    get_header();
    global $post;
    var_dump($post);
    die();

如果我尝试wp函数get_post(),它也返回null..

Thanks a lot

我通过替换定义插件路径的全局变量解决了这个问题。

我正在使用plugin_dir_url方法并且有一个url包含错误,所以我用plugin_dir_path替换了这个方法,这解决了我所有的矛盾。

最新更新