show related posts wordpress without plugin using category

How to show related posts wordpress without plugin using category. Showing related article in wordpress site is always good for users to attract visitors.  In many blogs people are showing the related or linked articles. Showing similier or related articles will increaze your site SEO. show related posts without wordpress plugin using category in wordpress site is always good for users to attract the visitors.

show related posts wordpress without plugin

More visitor will visit your site. For showing the related articles you need copy following code into your functions.php file.

[viral-lock message=”Code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

01// "Similier Articles"
02function wpapi_more_from_cat( $title = "Similier Articles:" ) {
03    global $post;
04    // We should get the first category of the post
05    $categories = get_the_category( $post->ID );
06    $first_cat = $categories[0]->cat_ID;
07    // Let's start the $output by displaying the title and opening the <ul>
08    $output = '<h3>' . $title . '</h3>';
09    // The arguments of the post list!
10    $args = array(
11        // It should be in the first category of our post:
12        'category__in' => array( $first_cat ),
13        // Our post should NOT be in the list:
14        'post__not_in' => array( $post->ID ),
15        // ...And it should fetch 5 posts - you can change this number if you like:
16        'posts_per_page' => 5
17    );
18    // The get_posts() function
19    $posts = get_posts( $args );
20    if( $posts ) {
21        $output .= '<ul>';
22        // Let's start the loop!
23        foreach( $posts as $post ) {
24            setup_postdata( $post );
25            $post_title = get_the_title();
26            $permalink = get_permalink();
27            $output .= '
28<ul>
29    <li>permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</li>
30</ul>
31';
32        }
33        $output .= '</ul>';
34    } else {
35        // If there are no posts, we should return something, too!
36        $output .= '<p>Sorry, this category has just one post and you just read it!</p>';
37    }
38    echo $output;
39}

[/viral-lock]

After that open your single.php file and use following code.

1wpapi_more_from_cat();
show related posts wordpress without plugin
show related posts wordpress without plugin

Using above code you can show the related articles in your wordpress site.

how to get post data outside loop in wordpress

WordPress hack, In this wordpress tutorial we given code and shown, how to get post data outside loop in wordpress. In wordpress theme you can get post data.

how to get post data outside loop in wordpress

Outside the loop you can get the a post data. you just need to copy paste following code in your theme file.

01<?php
02global $post;
03 
04$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
05 
06$myposts = get_posts( $args );
07 
08foreach( $myposts as $post ) : setup_postdata($post); ?>
09 <li>
10 <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
11 </li>
12<?php endforeach; ?>
how to get post data outside loop in wordpress
how to get post data outside loop in wordpress

most used wordpress functions in theme

In wordpress theming is very important. Developers know the importance of functions.php file. Here we given most used wordpress functions in theme which will be useful for wordpress developer. I always written some very nice code snippets in functions.php file.

most used wordpress functions in theme

I found very useful codes which is very helpful for very wordpress designer and developers.

Here is very useful code snippets.
Enable Hidden Admin Feature displaying ALL Site Settings

1// CUSTOM ADMIN MENU LINK FOR ALL SETTINGS
2   function all_settings_link() {
3    add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
4   }
5   add_action('admin_menu', 'all_settings_link');

Remove Update Notification for all users except ADMIN User

1// REMOVE THE WORDPRESS UPDATE NOTIFICATION FOR ALL USERS EXCEPT SYSADMIN
2       global $user_login;
3       get_currentuserinfo();
4       if (!current_user_can('update_plugins')) { // checks to see if current user can update plugins
5        add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
6        add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
7       }

Include custom post types in the search results.

1// MAKE CUSTOM POST TYPES SEARCHABLE
2function searchAll( $query ) {
3 if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
4 return $query;
5}
6add_filter( 'the_search_query', 'searchAll' );

Add your custom post types to your sites main RSS feed by default.

1// ADD CUSTOM POST TYPES TO THE DEFAULT RSS FEED
2function custom_feed_request( $vars ) {
3 if (isset($vars['feed']) && !isset($vars['post_type']))
4  $vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' );
5 return $vars;
6}
7add_filter( 'request', 'custom_feed_request' );

Modify the Login Logo & Image URL Link

01add_filter( 'login_headerurl', 'namespace_login_headerurl' );
02/**
03 * Replaces the login header logo URL
04 *
05 * @param $url
06 */
07function namespace_login_headerurl( $url ) {
08    $url = home_url( '/' );
09    return $url;
10}
11 
12add_filter( 'login_headertitle', 'namespace_login_headertitle' );
13/**
14 * Replaces the login header logo title
15 *
16 * @param $title
17 */
18function namespace_login_headertitle( $title ) {
19    $title = get_bloginfo( 'name' );
20    return $title;
21}
22 
23add_action( 'login_head', 'namespace_login_style' );
24/**
25 * Replaces the login header logo
26 */
27function namespace_login_style() {
28    echo '<style>.login h1 a { background-image: url( ' . get_template_directory_uri() . '/images/logo.png ) !important; }</style>';
29}

Loading jQuery from the Google CDN

01// even more smart jquery inclusion :)
02add_action( 'init', 'jquery_register' );
03 
04// register from google and for footer
05function jquery_register() {
06 
07if ( !is_admin() ) {
08 
09    wp_deregister_script( 'jquery' );
10    wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' ), false, null, true );
11    wp_enqueue_script( 'jquery' );
12}
13}

Remove the WordPress Version Info for Security

1// remove version info from head and feeds
2function complete_version_removal() {
3    return '';
4}
5add_filter('the_generator', 'complete_version_removal');

Add Spam & Delete Links to Comments on Front End

1// spam & delete links for all versions of wordpress
2function delete_comment_link($id) {
3    if (current_user_can('edit_post')) {
4        echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&c='.$id.'">del</a> ';
5        echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&dt=spam&c='.$id.'">spam</a>';
6    }
7}

Remove Default WordPress Meta Boxes

01// REMOVE META BOXES FROM DEFAULT POSTS SCREEN
02   function remove_default_post_screen_metaboxes() {
03 remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
04 remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
05 remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
06 remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
07 remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
08 remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
09 }
10   add_action('admin_menu','remove_default_post_screen_metaboxes');
11 
12// REMOVE META BOXES FROM DEFAULT PAGES SCREEN
13   function remove_default_page_screen_metaboxes() {
14 remove_meta_box( 'postcustom','page','normal' ); // Custom Fields Metabox
15 remove_meta_box( 'postexcerpt','page','normal' ); // Excerpt Metabox
16 remove_meta_box( 'commentstatusdiv','page','normal' ); // Comments Metabox
17 remove_meta_box( 'trackbacksdiv','page','normal' ); // Talkback Metabox
18 remove_meta_box( 'slugdiv','page','normal' ); // Slug Metabox
19 remove_meta_box( 'authordiv','page','normal' ); // Author Metabox
20 }
21   add_action('admin_menu','remove_default_page_screen_metaboxes');

Add Custom User Profile Fields

01// CUSTOM USER PROFILE FIELDS
02   function my_custom_userfields( $contactmethods ) {
03 
04    // ADD CONTACT CUSTOM FIELDS
05    $contactmethods['contact_phone_office']     = 'Office Phone';
06    $contactmethods['contact_phone_mobile']     = 'Mobile Phone';
07    $contactmethods['contact_office_fax']       = 'Office Fax';
08 
09    // ADD ADDRESS CUSTOM FIELDS
10    $contactmethods['address_line_1']       = 'Address Line 1';
11    $contactmethods['address_line_2']       = 'Address Line 2 (optional)';
12    $contactmethods['address_city']         = 'City';
13    $contactmethods['address_state']        = 'State';
14    $contactmethods['address_zipcode']      = 'Zipcode';
15    return $contactmethods;
16   }
17   add_filter('user_contactmethods','my_custom_userfields',10,1);

Add an excerpt box for pages

1if ( function_exists('add_post_type_support') )
2{
3    add_action('init', 'add_page_excerpts');
4    function add_page_excerpts()
5    {
6        add_post_type_support( 'page', 'excerpt' );
7    }
8}

Function to change the length of Exerpt

1function new_excerpt_length($length) {
2    return 100;
3}
4 
5add_filter('excerpt_length', 'new_excerpt_length');

Auto Extract the First Image from the Post Content

01/ AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST
02function getImage($num) {
03    global $more;
04    $more = 1;
05    $link = get_permalink();
06    $content = get_the_content();
07    $count = substr_count($content, '<img');
08    $start = 0;
09    for($i=1;$i<=$count;$i++) {
10        $imgBeg = strpos($content, '<img', $start);
11        $post = substr($content, $imgBeg);
12        $imgEnd = strpos($post, '>');
13        $postOutput = substr($post, 0, $imgEnd+1);
14        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
15        $image[$i] = $postOutput;
16        $start=$imgEnd+1;
17    }
18    if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
19    $more = 0;
20}

Unregister WP Default Widgets

01// unregister all default WP Widgets
02function unregister_default_wp_widgets() {
03    unregister_widget('WP_Widget_Pages');
04    unregister_widget('WP_Widget_Calendar');
05    unregister_widget('WP_Widget_Archives');
06    unregister_widget('WP_Widget_Links');
07    unregister_widget('WP_Widget_Meta');
08    unregister_widget('WP_Widget_Search');
09    unregister_widget('WP_Widget_Text');
10    unregister_widget('WP_Widget_Categories');
11    unregister_widget('WP_Widget_Recent_Posts');
12    unregister_widget('WP_Widget_Recent_Comments');
13    unregister_widget('WP_Widget_RSS');
14    unregister_widget('WP_Widget_Tag_Cloud');
15}
16add_action('widgets_init', 'unregister_default_wp_widgets', 1);

Enable GZIP output compression

1if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
2   add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));

Enable shortcodes in widgets

1// shortcode in widgets
2if ( !is_admin() ){
3    add_filter('widget_text', 'do_shortcode', 11);
4}

If you have any interesting code snippets then please suggest me.

most used wordpress functions in theme
most used wordpress functions in theme