最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - i can not access the post in custom template of my wordpress plugin

programmeradmin3浏览0评论

i added following code to use custom template for my recipes single page

function override_single_template( $single_template ){
    global $post;
    if ($post->post_type == "recipes"){
        $single_template  = plugins_url('/recipe-single-page-template.php',__FILE__);
    }
    return $single_template;
}
add_filter( 'single_template', 'override_single_template',10);

and in my template i added following code

<?php
/*
Template Name: recipe-single-page-template
Template Post Type: recipes
*/
require_once("../../../wp-load.php");
?>

<?php get_header(); ?>

<?php echo $post->ID?>

<?php get_footer(); ?>

but i do not access the post and echo out post id will cuses the following error

Trying to get property of non-object

var dump $post outputs null

NULL

and following code will print out my custom template address

$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_url;

top code result:

/wp-content/plugins/recipe-plugin/templates/single-recipes.php

now whate should i do?

发布评论

评论列表(0)

  1. 暂无评论