2

我正在使用 WordPress 帖子来构建帖子。当我没有为帖子选择类别时,是否有任何插件或某些功能会发出警告。每当我发布一个类别时,我都会错过一些时间,因为有很多一段时间后要在类别中发布帖子,看到那些未分类的帖子非常头疼。

add_theme_support('menus');

**

**> 上面的代码什么都不是,因为堆栈溢出不允许我

发布我的问题,因为它说标准低。**

** 提前致谢。希望我能找到一个合适的答案

4

2 回答 2

5

哈哈..它太有趣了,但你可以在你的functions.php文件中尝试如下

function force_post_categ_init() 
{
  wp_enqueue_script('jquery');
}
function force_post_categ() 
{
  echo "<script type='text/javascript'>\n";
  echo "
  jQuery('#publish').click(function() 
  {
    var cats = jQuery('[id^=\"taxonomy\"]')
      .find('.selectit')
      .find('input');
    category_selected=false;
    for (counter=0; counter<cats.length; counter++) 
    {
        if (cats.get(counter).checked==true) 
        {
            category_selected=true;
            break;
        }
    }
    if(category_selected==false) 
    {
      alert('You have not selected any category for the post. Please select post category.');
      setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
      jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
      setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
      return false;
    }
  });
  ";
   echo "</script>\n";
}
add_action('admin_init', 'force_post_categ_init');
add_action('edit_form_advanced', 'force_post_categ');

注意:-必须启用 javascript 才能运行它

于 2013-03-15T09:59:16.430 回答
2

我建议使用 jQuery 对象来读取输入的实际值,因为可以使用选中的值加载页面,然后可以取消选中并保存输入。这使用 jQuery 对象.is('checked')方法:

function force_post_categ() 
{
    $custom_js = <<<CUSTOM_JS
    <script type='text/javascript'>
    jQuery('#publish').click(function() 
    {

      var cats = jQuery('[id^="taxonomy"]').find('.selectit').find('input');
      category_selected = false;

      $.each(cats, function(key,value){
          if ( $(this).is(':checked') == true ) {
            category_selected = true;
            return false;
          }
      });

      if (category_selected == false) 
      {
        alert('You have not selected any metro or country for the post. Please select a metro.');
        setTimeout("jQuery('#ajax-loading').css('visibility', 'hidden');", 100);
        jQuery('[id^="taxonomy"]').find('.tabs-panel').css('background', '#F96');
        setTimeout("jQuery('#publish').removeClass('button-primary-disabled');", 100);
        return false;
      }
    });
    </script>
CUSTOM_JS;

    echo $custom_js;
}
于 2018-11-14T14:05:07.627 回答