How to add an admin function only to posts, not pages?

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…

Read More

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

Related posts

Leave a Reply

1 comment

  1. 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');