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.”]
// "Similier Articles"
function wpapi_more_from_cat( $title = "Similier Articles:" ) {
global $post;
// We should get the first category of the post
$categories = get_the_category( $post->ID );
$first_cat = $categories[0]->cat_ID;
// Let's start the $output by displaying the title and opening the <ul>
$output = '<h3>' . $title . '</h3>';
// The arguments of the post list!
$args = array(
// It should be in the first category of our post:
'category__in' => array( $first_cat ),
// Our post should NOT be in the list:
'post__not_in' => array( $post->ID ),
// ...And it should fetch 5 posts - you can change this number if you like:
'posts_per_page' => 5
);
// The get_posts() function
$posts = get_posts( $args );
if( $posts ) {
$output .= '<ul>';
// Let's start the loop!
foreach( $posts as $post ) {
setup_postdata( $post );
$post_title = get_the_title();
$permalink = get_permalink();
$output .= '
<ul>
<li>permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</li>
</ul>
';
}
$output .= '</ul>';
} else {
// If there are no posts, we should return something, too!
$output .= '<p>Sorry, this category has just one post and you just read it!</p>';
}
echo $output;
}
[/viral-lock]
After that open your single.php file and use following code.
wpapi_more_from_cat();
Using above code you can show the related articles in your wordpress site.
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.
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
// CUSTOM ADMIN MENU LINK FOR ALL SETTINGS
function all_settings_link() {
add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
}
add_action('admin_menu', 'all_settings_link');
Remove Update Notification for all users except ADMIN User
// REMOVE THE WORDPRESS UPDATE NOTIFICATION FOR ALL USERS EXCEPT SYSADMIN
global $user_login;
get_currentuserinfo();
if (!current_user_can('update_plugins')) { // checks to see if current user can update plugins
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}
Include custom post types in the search results.
// MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
return $query;
}
add_filter( 'the_search_query', 'searchAll' );
Add your custom post types to your sites main RSS feed by default.
// ADD CUSTOM POST TYPES TO THE DEFAULT RSS FEED
function custom_feed_request( $vars ) {
if (isset($vars['feed']) && !isset($vars['post_type']))
$vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' );
return $vars;
}
add_filter( 'request', 'custom_feed_request' );
Modify the Login Logo & Image URL Link
add_filter( 'login_headerurl', 'namespace_login_headerurl' );
/**
* Replaces the login header logo URL
*
* @param $url
*/
function namespace_login_headerurl( $url ) {
$url = home_url( '/' );
return $url;
}
add_filter( 'login_headertitle', 'namespace_login_headertitle' );
/**
* Replaces the login header logo title
*
* @param $title
*/
function namespace_login_headertitle( $title ) {
$title = get_bloginfo( 'name' );
return $title;
}
add_action( 'login_head', 'namespace_login_style' );
/**
* Replaces the login header logo
*/
function namespace_login_style() {
echo '<style>.login h1 a { background-image: url( ' . get_template_directory_uri() . '/images/logo.png ) !important; }</style>';
}
Loading jQuery from the Google CDN
// even more smart jquery inclusion :)
add_action( 'init', 'jquery_register' );
// register from google and for footer
function jquery_register() {
if ( !is_admin() ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' ), false, null, true );
wp_enqueue_script( 'jquery' );
}
}
Remove the WordPress Version Info for Security
// remove version info from head and feeds
function complete_version_removal() {
return '';
}
add_filter('the_generator', 'complete_version_removal');
Add Spam & Delete Links to Comments on Front End
// spam & delete links for all versions of wordpress
function delete_comment_link($id) {
if (current_user_can('edit_post')) {
echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&c='.$id.'">del</a> ';
echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&dt=spam&c='.$id.'">spam</a>';
}
}