将元框关联到 WordPress 中的自定义页面模板
Associate a Metabox to a custom page template in wordpress
我正在为wordpress制作模板组合,我在自定义页面的后端添加元框时遇到了一些困难。我有一个带有进度条的页面"关于我"来显示技能,我会在 bakend 中添加一个元框来更改进度条的百分比,现在我可以在所有页面的后端看到元框,如果页面地址与预期不符,我会通过 jquery 隐藏它, 有什么方法可以使显示更通用并将元框关联到自定义页面模板?
这是我在函数中的代码.php,它运行良好,但我会让它不那么具体
<?php
add_action('add_meta_boxes', 'box_percentuale');
function box_percentuale() {
add_meta_box(
'percentuale',
'Mdifica progress bar',
'box_percentuale_style',
'page'
);
}
function box_percentuale_style($post) {
?>
<div>
<h4>Percentuale skills</h4>
<label>HTML</label><input type="number" name="html" value="<?php echo get_post_meta($post->ID, 'html', true); ?>">
<label>CSS</label><input type="number" name="css" value="<?php echo get_post_meta($post->ID, 'css', true); ?>">
<label>JQUERY</label><input type="number" name="jquery" value="<?php echo get_post_meta($post->ID, 'jquery', true); ?>">
<label>WORDPRESS</label><input type="number" name="wordpress" value="<?php echo get_post_meta($post->ID, 'wordpress', true); ?>">
<label>ILLUSTRATOR</label><input type="number" name="illustrator" value="<?php echo get_post_meta($post->ID, 'illustrator', true); ?>">
<label>PHOTOSHOP</label><input type="number" name="photoshop" value="<?php echo get_post_meta($post->ID, 'photoshop', true); ?>">
</div>
<script>
jQuery(document).ready(function($) {
var url = window.location.href;
var urlPagina = 'http://localhost/wordpress/wp-admin/post.php?post=34&action=edit';
$('#percentuale').css('display', 'none');
if (url == urlPagina) {
$('#percentuale').show();
};
});
</script>
<?php
}
add_action('save_post', 'slava_percentuale');
function slava_percentuale($post_id)
{
if(isset($_POST['yiw_progetti_link'])) {
update_post_meta($post_id, 'html', intval($_POST['html']));
update_post_meta($post_id, 'css', intval($_POST['css']));
update_post_meta($post_id, 'jquery', intval($_POST['jquery']));
update_post_meta($post_id, 'wordpress', intval($_POST['wordpress']));
update_post_meta($post_id, 'illustrator', intval($_POST['illustrator']));
update_post_meta($post_id, 'photoshop', intval($_POST['photoshop']));
}
}
?>
您可以使用以下内容来实现:
function box_percentuale() {
global $post;
// This will check if you are actually on the page's editing screen
if ( ! empty( $post ) ) {
// This will get the page template of the page you are editing
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// Check if you are using the template you wish to add meta box to
// Simply change "about.php" to the name of your template
// Note: if the template is in a subdirectory, you have to add it as well
// Example: 'page-templates/about.php'
if ( $page_template == 'about.php' ) {
add_meta_box(
'percentuale',
'Mdifica progress bar',
'box_percentuale_style',
'page'
);
}
}
}
add_action('add_meta_boxes', 'box_percentuale');
快速说明:如果您转到具有默认页面模板的页面并从模板下拉列表中选择"关于",则必须首先保存页面才能显示元框(即,在您更改下拉列表的那一刻它不会出现)。我不相信这对你来说会是一个问题,但我认为值得一提。
相关文章:
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- jQuery自定义验证比较多个输入的序列
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 在wordpress一定时间后更改自定义字段
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 自定义函数中的光标位置
- 用于检查数组中是否存在元素的javascript自定义方法
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- Meteor-添加用户自定义字段的方法不起作用
- 自定义表单验证和提交
- jQuery工具验证器自定义效果-添加&消除影响
- 可以't计算自定义谷歌地图的js
- 谷歌水印未显示在自定义搜索框中
- vaadin:使用自定义布局集成angular js
- 将元框关联到 WordPress 中的自定义页面模板
- 将自定义用户代理关联到特定的谷歌浏览器页面/标签页
- 单击自定义复选框不会运行关联的onClick功能
- 自定义javascript对象排序和维护索引关联
- 在Javascript中创建一个具有自定义获取/放置操作的关联数组