仅在带有前向链接的post-new.php上加载一个meta box

时间:2013-03-06 作者:Tim Truston

我需要加载一个元框,以便编辑人员在编辑之前首先选择父帖子。但是没有看到我期望的宽元框,正常的编辑页面只是加载:这是wp admin/post new开头的代码。php:

            if ( $post_type == \'news\' && !isset($_GET[\'parent_id\']) ):
                       //checking if parent_id exists on querystring

            //Give them the news meta box only
            add_action(\'add_meta_boxes\', function() { add_meta_box(\'news-parent\', \'Please select the Parent Project for this news:\', \'news_attributes_meta_box\', \'news\', \'normal\', \'high\');});//add wide meta box

              function news_attributes_meta_box($post) {
                  $parentselectbox = wp_dropdown_pages(array(
                  \'post_type\' => \'project\',
                  \'name\' => \'parent_id\',
                  \'show_option_none\' => __(\'(no parent)\'),
                  \'sort_column\'=> \'menu_order,
                  post_title\',
                  \'echo\' => 0
                  ));
                  if ( ! empty($parentselectbox) ) {
                    echo $parentselectbox;
                  } // end empty pages check
                };

            exit;//stop page load after meta box is loaded
            //if chosen, give a move forward link with parent_id on querystring
            endif;

1 个回复
最合适的回答,由SO网友:bcorkins 整理而成

您不应该修改任何WordPress核心文件,您的更改将在下一次WordPress更新发布时丢失。

最简单的方法是使用高级自定义字段,创建“关系”自定义字段类型并将其作为必填字段。这样,用户就无法在不选择“父帖子”的情况下保存帖子。

但是,如果您确实需要在加载其余编辑窗口之前显示元框(我建议不要这样做,因为我认为它破坏了大多数人所期望的标准界面),您仍然可以在函数中创建post元框。php文件。请参见此处,以获取精彩的教程:http://wp.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/.

一旦拥有了metabox,就可以使用一些简单的javascript隐藏所有页面元素,直到在metabox中选择了某些内容,例如(未测试):

$(document).ready(function() {
    $(\'div:not(#mypostboxid)\').fadeTo("slow", 0.33); // fade the page to .33 opacity
    $(\'div:not(#mypostboxid)\').find(\'input, textarea, button, select\').attr(\'disabled\',\'disabled\'); // disable all other inputs
    $(\'.myselectbox\').change(function() {
         $(\'div:not(#mypostboxid)\').fadeTo("slow", 1); // fade back to visible
         $(\'div:not(#mypostboxid)\').find(\'input, textarea, button, select\').attr(\'disabled\',\'enabled\'); // re-enable inputs
    }
});

结束

相关推荐

创建自定义分类并在Metabox下拉列表中显示

我想在编辑后的屏幕上创建一个下拉列表,其中包含3个已经存在的标签。最简单的方法是什么?基本上,我所要寻找的只是一个简单的下拉列表,它将以下标签之一添加到帖子本身中;\'炖牛肉、豌豆汤辣椒\'。我还希望“炖牛肉”是默认的。提前谢谢你编辑:由于我不希望用户在任何时候都能够显示这三个类别中的一个以上,并且必须选择根据自己的意愿轻松更改哪一个类别,所以标签可能不是最好的解决方案?创建自定义分类法(“食物”)会更好吗?它们基本上用于改变帖子在网站首页上的显示方式。