在wordpress中添加添加新帖子页面
在WP 4.2中打开添加新帖子页面并且没有编辑任何字段,单击“发布”按钮页面被提交并重新打开,但没有任何字段被标记为红色背景颜色根据需要。 如何制作它?看起来,这是这个页面的原始行为。 我希望它能够像类别编辑器一样工作,在类似情况下,字段名称会根据需要用红色背景色标记。在wordpress中添加添加新帖子页面
此外,我使用register_post_type函数添加了几个字段到新邮政页面,我也希望根据需要用红色背景色标记。这是最好的办法吗?
谢谢!
您可以使用下面的代码由你自己打电话给你的jQuery验证,
在你的主题添加在主题的functions.php
和custom.js
这个代码js
文件夹
add_action('admin_print_scripts-post-new.php', 'custom_admin_script', 11);
add_action('admin_print_scripts-post.php', 'custom_admin_script', 11);
function custom_admin_script() {
global $post_type;
// Post type = "post" or "page" you can load script wheater it is post or page , if page then change 'post' to 'page' in below condition
if('post' == $post_type)
wp_enqueue_script('custom-admin-script', get_stylesheet_directory_uri() . '/js/custom.js');
}
custom.js
jQuery(document).ready(function ($) {
if ($("#post").length > 0) {
var frm = true;
$("#publish").click(function() {
var title = $("#title").val();
var excerpt = $("#excerpt").val();
// your custom field variable goes here
if (jQuery.trim(title) == "" || jQuery.trim(excerpt) == "") {
frm = false;
}
if (frm == false) {
$("#publish").prop('disabled', true);
} else {
$("#publish").prop('disabled', false);
}
});
}
});
感谢您的帮助! 插件必填字段是有用的,但似乎它不适用于使用register_post_type函数创建的自定义类型。 我会尝试编辑它为我的情况... 此外,RTF编辑器(CKEditor在我的情况下,因为我有“CKEditor for WordPress”插件安装)的id和名称值的问题,以获得其值(code#) [code] $(“#post_content”)。val(); ? 至于在我的情况下的JS脚本,我不需要禁用“发布按钮,我更需要停止提交页面。如果有这种方式? –
是否要检查发布内容区域为空?你还想知道显示编辑器的'post content'区域的'ID'? – Noman
还必须设置发布按钮被禁用,直到所有字段都不是空的,如果你不这样做,那么你的验证是无用的 – Noman
试试这个[plugin](https://wordpress.org/plugins/mandatory-fields/) –