All posts by acrane

About acrane

A web designer from Charlotte, NC, I enjoy working in the yard, kayaking, building stuff, making things, creating, thinking and of course WordPress.

How to add Custom Post Types

You can add a custom Post type to your WordPress theme by including the following in your theme’s functions file. Just change the names to match what you need.

add_action('init', 'js_custom_init');
function js_custom_init() 
{
  $labels = array(
	'name' => _x('Banner', 'post type general name'),
    'singular_name' => _x('Banner', 'post type singular name'),
    'add_new' => _x('Add New', 'banner'),
    'add_new_item' => __('Add New Banner'),
    'edit_item' => __('Edit Banner'),
    'new_item' => __('New Banner'),
    'view_item' => __('View Banner'),
    'search_items' => __('Search Banner'),
    'not_found' =>  __('No slides found'),
    'not_found_in_trash' => __('No slides found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Banner'
  );
  $args = array(
	'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array('title','editor','custom-fields','thumbnail')
  ); 
  register_post_type('banner',$args);  
}

Once you’ve included the function, you can call it in your theme by adding a custom post type query.

 'episode', 'posts_per_page' => 1 );
	$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>

All of your amazing stuff...

How to Change the Names of Menu items in the WordPress Backend

What are “Posts”? Sounds like a silly question for an everyday WordPress user. But for the client that may not be accustom to everyday WordPress lingo it gets downright confusing. So, let’s change the names! Simple enough. Let’s add some functions to your theme’s function.php file.

This function changes the “Menu Name”. So in this example, we are changing, “Posts” to “Blog Entries”.

// Changing WordPress admin Menu Names
function change_post_menu_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'Blog Entries';
    $submenu['edit.php'][5][0] = 'Blog Entries';
    $submenu['edit.php'][10][0] = 'Add a Blog Entry';
    echo '';
}
add_action( 'admin_menu', 'change_post_menu_label' );

You can even change the name, “Categories” and “Tags” to fit your specific project:

// Changing WordPress admin Menu Names
function change_post_menu_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'Blog Entries';
    $submenu['edit.php'][5][0] = 'Blog Entries';
    $submenu['edit.php'][10][0] = 'Add a Blog Entry';
    $submenu['edit.php'][15][0] = 'Status'; // Change name for categories
    $submenu['edit.php'][16][0] = 'Labels'; // Change name for tags
    echo '';
}
add_action( 'admin_menu', 'change_post_menu_label' );

You can also change the way the post object is edited and it’s names.

function change_post_object_label() {
        global $wp_post_types;
        $labels = &$wp_post_types['post']->labels;
        $labels->name = 'Blog Posts';
        $labels->singular_name = 'Blog Entry';
        $labels->add_new = 'Add a Blog Entry';
        $labels->add_new_item = 'Add a Blog Entry';
        $labels->edit_item = 'Edit Blog Entries';
        $labels->new_item = 'Blog Entries';
        $labels->view_item = 'View Blog Entries';
        $labels->search_items = 'Search Blog Entries';
        $labels->not_found = 'No Blog Entries found';
        $labels->not_found_in_trash = 'No Blog Entries found in Trash';
    }
add_action( 'init', 'change_post_object_label' );

How to remove Menu Items that Clients don’t need.

WordPress is great. It has so much functionality that it can get overwhelming especially for clients. Clean up the backend with a couple of simple functions added to the functions.php file in your theme folder. I’ve gone ahead and added a couple, but you can edit the array to your hearts desire.

// Remove unwanted menu Items 
function remove_menus () {
global $menu;
	$restricted = array(
	__('Dashboard'), 
	__('Tools'),
	__('Comments'));
	end ($menu);
	while (prev($menu)){
		$value = explode(' ',$menu[key($menu)][0]);
		if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
	}
}
add_action('admin_menu', 'remove_menus');

How to add a Custom Login Logo for Clients

Add the following to your theme’s functions.php file. Also remember to add your login logo (png) into your theme’s “images” folder.

function custom_login_logo() {
        echo '';
}
add_action('login_head', 'custom_login_logo'); 

function my_login_logo_url() {
    return get_bloginfo( 'url' );
}
add_filter( 'login_headerurl', 'my_login_logo_url' );

function my_login_logo_url_title() {
    return get_bloginfo('name');
}
add_filter( 'login_headertitle', 'my_login_logo_url_title' ); 

How to add a custom length excerpt without truncating the last word.

Place the following in your theme’s functions.php file.

// Excerpt Function
function get_excerpt($count){
  // whatever you want to append on the end of the last word
  $words = '...';
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = wp_trim_words($excerpt, $count, $words);
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

 

or this… without the more permalink at the end:

// Excerpt Function
function get_excerpt($count){
  // whatever you want to append on the end of the last word
  $words = '...';
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = wp_trim_words($excerpt, $count, $words);
  return $excerpt;
}

 

Paste this in your theme to display the custom excerpt.

// change the # to lengthen or shorten your excerpt
echo get_excerpt(125);