我让人编写了一个我设计的自定义主题。我需要复制这个人制作的一个模板,为CSS提供唯一的类名,根据需要调整CSS,最后将其保存为一个全新的模板。
What I have done so far:
<打开了我想在Dreamweaver中复制的模板。
将页面顶部的模板名称更改为新模板的名称。
由于模板将成为新模板,因此以其他名称保存模板。
通过FTP将模板保存到服务器上剩余模板所在的位置。Here\'s the problem:
如果我创建一个新页面,并选择我的新模板,那么自定义元框(cmb)不会像我选择复制新模板的模板时那样显示。它只显示默认的元框。看看这些截图
下面是原始模板(template.storage.php)和我的新模板的代码。请记住,这些模板是完全相同的,我只是更改了模板名称。稍后我将更改一些类以添加自定义样式,但这与现在无关。
<?php
/**
Template Name: Storage
**/
?>
<?php get_header(); ?>
<?php $container_background = get_post_meta($post->ID, \'_cmb_background-container\', true); ?>
<?php
$box_one_heading = get_post_meta($post->ID, \'_cmb_storage-box-one-heading\', true);
$box_one_content = wpautop(get_post_meta($post->ID, \'_cmb_storage-box-one-content\', true));
$box_two_heading = get_post_meta($post->ID, \'_cmb_storage-box-two-heading\', true);
$box_two_content = wpautop(get_post_meta($post->ID, \'_cmb_storage-box-two-content\', true));
?>
<div id="container" class="innerpage" style="background: #fff3cb url(\'<?php echo ($container_background ? $container_background : "".get_stylesheet_directory_uri()."/images/corks.png"); ?>\') center bottom no-repeat;">
<?php fourosix_breadcrumbs(); ?>
<div id="content" class="linkstable">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div style="width:44%; float:left; ">
<?php if($box_one_heading && $box_one_content) { ?>
<div class="pagebox shadow">
<h1 class="widget-title titleleft"><?php echo $box_one_heading; ?></h1>
<?php echo $box_one_content; ?>
</div>
<?php } ?>
<?php if($box_two_heading && $box_two_content) { ?>
<div class="pagebox shadow" style="margin-top:27px;">
<h1 class="widget-title titleleft"><?php echo $box_two_heading; ?></h1>
<?php echo $box_two_content; ?>
</div>
<?php } ?>
</div>
<div style="width:48%; float:left;">
<div class="pagebox shadow contactbox">
<?php the_post_thumbnail(485,320,true); ?>
</div>
<div class="pagebox shadow contactbox">
<?php $beerimage = get_post_meta($post->ID, \'BeerImage\', true); ?>
<!--<img src="<?php echo $beerimage; ?>" class="wp-post-image"/>-->
</div>
</div>
<?php if ( is_user_logged_in() ) : ?>
<footer>
<?php edit_post_link( __( \'edit\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
</footer>
<?php endif; ?>
</article><!-- end post-->
<?php endwhile; ?>
<?php else : ?>
<article class="post">
<h1>page not found</h1>
</article>
<?php endif; ?>
</div><!-- end content -->
</div><!-- end container -->
<?php get_footer(); ?>
</body>
</html>
以下是复制模板的标题:
<?php
/**
Template Name: Testimonials
**/
?>
我还包括这两段代码,因为这可能会有所帮助
“enable.php”中的代码
<?php
/**
* Include and setup custom metaboxes and fields.
*
* @category YourThemeOrPlugin
* @package Metaboxes
* @license http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
* @link https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
*/
add_filter( \'cmb_meta_boxes\', \'wsod_metaboxes\' );
/**
* Define the metabox and field configurations.
*
* @param array $meta_boxes
* @return array
*/
function wsod_metaboxes( array $meta_boxes ) {
// Start with an underscore to hide fields from custom fields list
$prefix = \'_cmb_\';
$meta_boxes[] = array(
\'id\' => \'general-settings\',
\'title\' => \'General Settings\',
\'pages\' => array(\'page\'),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'show_names\' => \'true\',
\'fields\' => array(
array(
\'name\' => \'Background Container\',
\'desc\' => \'Please upload the image you would like to use as background for the container of this page\',
\'id\' => $prefix . \'background-container\',
\'type\' => \'file\'
)
)
);
$meta_boxes[] = array(
\'id\' => \'page-storage\',
\'title\' => \'Storage Content\',
\'pages\' => array(\'page\'),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'show_names\' => \'true\',
\'fields\' => array(
array(
\'name\' => \'Box #1 Heading\',
\'desc\' => \'Please input the heading of Box#1 here\',
\'id\' => $prefix . \'storage-box-one-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #1 Content\',
\'desc\' => \'Please input the content of Box#1 here\',
\'id\' => $prefix . \'storage-box-one-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, )
),
array(
\'name\' => \'Box #2 Heading\',
\'desc\' => \'Please input the heading of Box#2 here\',
\'id\' => $prefix . \'storage-box-two-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #2 Content\',
\'desc\' => \'Please input the content of Box#2 here\',
\'id\' => $prefix . \'storage-box-two-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, )
)
)
);
$meta_boxes[] = array(
\'id\' => \'page-services\',
\'title\' => \'Services Content\',
\'pages\' => array(\'page\'),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'show_names\' => \'true\',
\'fields\' => array(
array(
\'name\' => \'Box #1 Heading\',
\'desc\' => \'Please input the heading of Box#1 here\',
\'id\' => $prefix . \'services-box-one-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #1 Content\',
\'desc\' => \'Please input the text content of Box#1 here\',
\'id\' => $prefix . \'services-box-one-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, ),
),
array(
\'name\' => \'Box #2 Heading\',
\'desc\' => \'Please input the heading of Box#2 here\',
\'id\' => $prefix . \'services-box-two-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box#2 Content\',
\'desc\' => \'Please input the text content of Box#2 here\',
\'id\' => $prefix . \'services-box-two-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, ),
),
array(
\'name\' => \'Box #3 Heading\',
\'desc\' => \'Please input the heading of Box#3 here\',
\'id\' => $prefix . \'services-box-three-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box#3 Content\',
\'desc\' => \'Please input the text content of Box#3 here\',
\'id\' => $prefix . \'services-box-three-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, ),
),
array(
\'name\' => \'Box #4 Heading\',
\'desc\' => \'Please input the heading of Box#4 here\',
\'id\' => $prefix . \'services-box-four-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box#4 Content\',
\'desc\' => \'Please input the text content of Box#4 here\',
\'id\' => $prefix . \'services-box-four-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, ),
),
)
);
// Rest of code
return $meta_boxes;
}
add_action( \'init\', \'cmb_initialize_cmb_meta_boxes\', 9999 );
/**
* Initialize the metabox class.
*/
function cmb_initialize_cmb_meta_boxes() {
if ( ! class_exists( \'cmb_Meta_Box\' ) )
require_once \'init.php\';
}
我的“script metabox template toggle.js”中的代码
jQuery(document).ready(function() {
// in this functions hide all the custom metaboxes that are used
function hide_all_custom_metaboxes() {
jQuery(\'#page-facilities\').hide();
jQuery(\'#page-services\').hide();
jQuery(\'#page-storage\').hide();
}
function show_custom_metabox(meta_id) {
var selector = "#" + meta_id;
if(jQuery(selector).length)
jQuery(selector).show();
}
function toggle_wp_editor(current_metabox) {
jQuery(\'#postdivrich\').show();
switch(current_metabox) {
case \'facility\':
//jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-facilities");
break;
case \'services\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-services");
break;
case \'storage\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-storage");
break;
}
}
function get_template_html_id() {
current_metabox = jQuery(\'#page_template option:selected\').val();
current_metabox = current_metabox.replace(\'template.\',\'\');
current_metabox = current_metabox.replace(\'.php\',\'\');
/*if(current_metabox == "default") {
current_metabox = "content-default";
}*/
return current_metabox;
}
hide_all_custom_metaboxes();
var current_metabox = get_template_html_id();
show_custom_metabox(current_metabox);
toggle_wp_editor(current_metabox);
jQuery(\'#page_template\').bind("change", function() {
hide_all_custom_metaboxes();
show_custom_metabox(get_template_html_id());
toggle_wp_editor(get_template_html_id());
});
});
关于如何使用自定义模板有什么建议吗?
最合适的回答,由SO网友:Pieter Goosen 整理而成
您的主题使用特定于某些页面模板的元盒,并且在其他页面模板中也同样隐藏(由jquery完成),这就是为什么您的新页面模板无法按预期工作的原因。
Here is how to solve your problem.:
将您的推荐模板称为“template.estimational.php”。由于您已经从存储模板中复制了代码,并相应地更改了模板名称,请打开模板并将“存储”的所有实例更改为“证明”。这将防止以后出现元数据库问题。
在第40到75行的“enable.php”中,您有以下代码
$meta_boxes[] = array(
\'id\' => \'page-storage\',
\'title\' => \'Storage Content\',
\'pages\' => array(\'page\'),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'show_names\' => \'true\',
\'fields\' => array(
array(
\'name\' => \'Box #1 Heading\',
\'desc\' => \'Please input the heading of Box#1 here\',
\'id\' => $prefix . \'storage-box-one-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #1 Content\',
\'desc\' => \'Please input the content of Box#1 here\',
\'id\' => $prefix . \'storage-box-one-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, )
),
array(
\'name\' => \'Box #2 Heading\',
\'desc\' => \'Please input the heading of Box#2 here\',
\'id\' => $prefix . \'storage-box-two-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #2 Content\',
\'desc\' => \'Please input the content of Box#2 here\',
\'id\' => $prefix . \'storage-box-two-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, )
)
)
);
您需要复制该代码,将其粘贴到第75行的正下方,并将所有存储实例更改为如下证明
$meta_boxes[] = array(
\'id\' => \'page-testimonials\',
\'title\' => \'Testimonials Content\',
\'pages\' => array(\'page\'),
\'context\' => \'normal\',
\'priority\' => \'high\',
\'show_names\' => \'true\',
\'fields\' => array(
array(
\'name\' => \'Box #1 Heading\',
\'desc\' => \'Please input the heading of Box#1 here\',
\'id\' => $prefix . \'testimonials-box-one-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #1 Content\',
\'desc\' => \'Please input the content of Box#1 here\',
\'id\' => $prefix . \'testimonials-box-one-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, )
),
array(
\'name\' => \'Box #2 Heading\',
\'desc\' => \'Please input the heading of Box#2 here\',
\'id\' => $prefix . \'testimonials-box-two-heading\',
\'type\' => \'text_medium\'
),
array(
\'name\' => \'Box #2 Content\',
\'desc\' => \'Please input the content of Box#2 here\',
\'id\' => $prefix . \'testimonials-box-two-content\',
\'type\' => \'wysiwyg\',
\'options\' => array( \'textarea_rows\' => 6, )
)
)
);
现在,您必须将该功能添加到js文件中,以便显示正确的元盒,并将其隐藏在正确的页面模板上。
现在打开“script metabox template toggle.js”。
添加以下行jQuery(\'#page-testimonials\').hide();
到
function hide_all_custom_metaboxes() {
jQuery(\'#page-facilities\').hide();
jQuery(\'#page-services\').hide();
jQuery(\'#page-storage\').hide();
}
然后是以下代码
case \'testimonials\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-testimonials");
break;
就在下面
case \'storage\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-storage");
break;
所以您完成的代码应该是
jQuery(document).ready(function() {
// in this functions hide all the custom metaboxes that are used
function hide_all_custom_metaboxes() {
jQuery(\'#page-facilities\').hide();
jQuery(\'#page-services\').hide();
jQuery(\'#page-storage\').hide();
jQuery(\'#page-testimonials\').hide();
}
function show_custom_metabox(meta_id) {
var selector = "#" + meta_id;
if(jQuery(selector).length)
jQuery(selector).show();
}
function toggle_wp_editor(current_metabox) {
jQuery(\'#postdivrich\').show();
switch(current_metabox) {
case \'facility\':
//jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-facilities");
break;
case \'services\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-services");
break;
case \'storage\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-storage");
break;
case \'testimonials\':
jQuery(\'#postdivrich\').css("display","none");
show_custom_metabox("page-testimonials");
break;
}
}
function get_template_html_id() {
current_metabox = jQuery(\'#page_template option:selected\').val();
current_metabox = current_metabox.replace(\'template.\',\'\');
current_metabox = current_metabox.replace(\'.php\',\'\');
/*if(current_metabox == "default") {
current_metabox = "content-default";
}*/
return current_metabox;
}
hide_all_custom_metaboxes();
var current_metabox = get_template_html_id();
show_custom_metabox(current_metabox);
toggle_wp_editor(current_metabox);
jQuery(\'#page_template\').bind("change", function() {
hide_all_custom_metaboxes();
show_custom_metabox(get_template_html_id());
toggle_wp_editor(get_template_html_id());
});
});
这应该可以奏效。如果你遇到任何问题,请告诉我。