1

I'm having a problem with adding an excerpt counter to the excerpt field. It may be a conflict with another plugin, but basically it screws with other jquery if the excerpt field doesn't exist (e.g.: when it's a page which doesn't have an excerpt).

Some issues that occur are: kills admin nav fly-out, media button doesn't work...

Is there a conditional I could use that would only add this function if it is a post, or if it has the excerpt field? I have several custom post types, and the default posts all using the excerpt field.

Here is the code I have so far, but I can't figure out what conditional would make this work:

function excerpt_count_js(){
    if(possible_conditional_here()) {
          echo '<script>jQuery(document).ready(function(){
    jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:0px;right:5px;color:#666;\"><small>Character limit = 150. Current characters: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_counter\" readonly=\"\" style=\"background:#fff;\">&nbsp;</div>");
         jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
         jQuery("#excerpt").keyup( function() {
         jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
       });
    });</script>';
      }
      return;
}
add_action( 'admin_head-post.php', 'excerpt_count_js');
add_action( 'admin_head-post-new.php', 'excerpt_count_js');

I've tried these (none of which worked):

if(post_type_exists()) - doesn't display at all
if(!is_page()) - doesn't change anything

2
  • Have you checked the console for any javascript errors?
    – John
    Commented Feb 5, 2013 at 17:47
  • Yes - when/because there is no excerpt field: >jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length); Commented Feb 5, 2013 at 18:29

1 Answer 1

2

You can use post type conditional tag:

if ( 'post' == get_post_type() )

Complete:

function excerpt_count_js(){
    if ( 'post' == get_post_type() ) {
          echo '<script>jQuery(document).ready(function(){
    jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:0px;right:5px;color:#666;\"><small>Character limit = 150. Current characters: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"excerpt_counter\" readonly=\"\" style=\"background:#fff;\">&nbsp;</div>");
         jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
         jQuery("#excerpt").keyup( function() {
         jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length);
       });
    });</script>';
      }
      return;
}
add_action( 'admin_head-post.php', 'excerpt_count_js');
add_action( 'admin_head-post-new.php', 'excerpt_count_js');
2
  • I think they want 'page' != get_post_type() to add it on all post types except page.
    – Milo
    Commented Feb 5, 2013 at 18:00
  • Rafael thanks so much - works perfectly and no more errors!!! Commented Feb 5, 2013 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.