首次构建Wordpress
插件。我创建了一个页面,并想从我的插件文件夹中为其分配页面模板。我的代码在页面编辑器的可选列表中显示了模板名称,但未附加到页面上。也无需手动将任何内容附加到页面上。但是文件路径正确。
我创建页面的代码:
$page_path = "this-is-a-campaign-landing-page";
$page_title = 'This is a Campaigns Page Title';
$page_content = 'THIS IS A CAMPAIGNS PAGE BODY';
$page_check = get_page_by_title( $page_path );
$page = array(
'post_type' => 'page',
'post_title' => $page_title,
'post_content' => $page_content,
'post_status' => 'publish',
'post_author' => $author->ID,
'post_slug' => $page_path
);
if (!isset($page_check->ID) && !get_page_by_path($page_path)) {
$page_id = wp_insert_post($page);
if ($page_id) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
update_post_meta($page_id, '_wp_page_template', $template );
}
}
我分别使用add_filters
function add_campaign_template ($templates) {
$templates['campaign_page.php'] = 'Campaign Page';
return $templates;
}
add_filter ('theme_page_templates', 'add_campaign_template');
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
页面创建没有错误。当我查看或编辑页面时,Default template
被选中,并且我的模板出现在列表中。手动选择它无效。我想念什么?
我的简单页面模板:
<?php
/**
* Template Name: Campaign Page
*
* @package PM
*/
// get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<section class="outer-categories">
<div class="container-fluid">
<div class="row text-justify">
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
<div class="col-lg-12">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'page' );
endwhile; // End of the loop.
?>
</div>
</div>
</div>
</section>
</main><!-- #main -->
</div><!-- #primary -->
<?php
// get_footer();
为什么不添加模板?启用debug
后,我在debug.log
中没有错误
答案 0 :(得分:0)
哇,漫长的一天。
更改此:
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
对此:
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = 'campaign_page.php'; <---- FIX
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');