Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我无法访问wordpress插件的自定义模板中的帖子_Php_Wordpress_Plugins - Fatal编程技术网

Php 我无法访问wordpress插件的自定义模板中的帖子

Php 我无法访问wordpress插件的自定义模板中的帖子,php,wordpress,plugins,Php,Wordpress,Plugins,我添加了以下代码,以便在我的食谱单页中使用自定义模板 function override_single_template( $single_template ){ global $post; if ($post->post_type == "recipes"){ $single_template = plugins_url('/recipe-single-page-template.php',__FILE__); } r

我添加了以下代码,以便在我的食谱单页中使用自定义模板

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);
在我的模板中,我添加了以下代码

<?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(); ?>
var dump$post输出为空

NULL
下面的代码将打印出我的自定义模板地址

$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_url;
顶级代码结果:

https://charter.test/wp-content/plugins/recipe-plugin/templates/single-recipes.php

现在我该怎么办?

您是否正在尝试将名为recipes的自定义帖子类型重定向到自定义模板?您不需要重写,默认情况下wordpress有一个内置的动态内容显示系统

在您的情况下,在显示配方时,它将首先搜索
single recipe.php
,如果未找到,则返回
single.php
,如果未找到,则返回
404.php
,最后返回
index.php

您只需创建一个名为
single recipe.php
的文件

您的第二个问题是,文件中没有显示循环,您必须告诉wordpress,如果存在帖子,它应该检索并呈现给您。为此,我们使用循环系统

您的
single recipe.php
文件应该如下所示:

<?php
/**
* Get theme header
*/
get_header();

/**
* Start loop
*/
if ( have_posts() ):
while ( have_posts() ):
the_post();

/**
* If we find a post, output the tilte
*/
echo the_title().'<br/>';

/**
* End loop
*/
endwhile; 
endif;

/**
* Get theme footer
*/
get_footer(); ?>

感谢您的answare。这对我不起作用。我试图添加我的自定义模板插件,而不是从他们的文件夹。我在模板文件中添加了你的代码,但没有显示任何内容
<?php
/**
* Get theme header
*/
get_header();

/**
* Start loop
*/
if ( have_posts() ):
while ( have_posts() ):
the_post();

/**
* If we find a post, output the tilte
*/
echo the_title().'<br/>';

/**
* End loop
*/
endwhile; 
endif;

/**
* Get theme footer
*/
get_footer(); ?>