Some WordPress Custom Functions For Custom Themes



Published On Thursday June 4, 2020 Reading Time: < 1 minute

WordPress is itself a complete content management system and we can tweak it in anyway we feel easy of. So, here some additional function we can put it in our function.js file so that the work becomes more easy while developing custom themes in WordPress.

Restrict The Amount of Text To Appear – Updated Form of the_excerpt function
Just echo the function with the amount of word required

function get_excerpt($count){
     $permalink = get_permalink($post->ID);
     $excerpt = get_the_content();
     $excerpt = strip_tags($excerpt);
     $excerpt = substr($excerpt, 0, $count);
     $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
     $excerpt = $excerpt.'..';
     return $excerpt;
}
//echo get_excerpt(500);

The second one hides the category title in archive page i.e. Category : Title

add_filter( 'get_the_archive_title', function ($title) {
      if ( is_category() ) {
            $title = single_cat_title( '', false );
       } elseif ( is_tag() ) {
            $title = single_tag_title( '', false );
       } elseif ( is_author() ) {
            $title = '<span class="vcard">' . get_the_author() . '</span>' ;
      }
      return $title;
 });

Add class active to the active menu

add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
     function special_nav_class ($classes, $item) {
     if (in_array('current-menu-item', $classes) ){
          $classes[] = 'active ';
     }
     return $classes;
}

 

Add a time ago, like 14hrs Ago

function time_ago( $type = 'post' ) {
     $date = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
     return human_time_diff($date('U'), current_time('timestamp')) . " " . __('ago');
}

 

 
0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
honey sing
honey sing
4 years ago

nice blog

2
0
Would love your thoughts, please comment.x
()
x