Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Have you checked the console for any javascript errors? –  John Feb 5 '13 at 17:47
    
Yes - when/because there is no excerpt field: >jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length); –  ByteMyPixel Feb 5 '13 at 18:29

1 Answer 1

up vote 1 down vote accepted

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');
share|improve this answer
    
I think they want 'page' != get_post_type() to add it on all post types except page. –  Milo Feb 5 '13 at 18:00
    
Rafael thanks so much - works perfectly and no more errors!!! –  ByteMyPixel Feb 5 '13 at 18:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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