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


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

Template Name: recipe-single-page-template
Template Post Type: recipes

<?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


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:


now whate should i do?

Amir Ramezanpour 4 months 0 Answers 20 views 0

Leave an answer