Here are some of the coolest WordPress tips and code snippets that can help you enhance your WordPress site—add social media to author profiles, show blog and page content on the same homepage, customize search behavior, and more—all with minimal effort and coding.
Include options to associate Social channels to Author profile
By default, WordPress author profiles don’t support social links.
Step 1: Add this snippet to your functions.php
to add Twitter, Facebook, and LinkedIn fields:
function social_contactmethods( $contactmethods ) { // Add Twitter $contactmethods['twitter'] = 'Twitter'; //Add Facebook $contactmethods['facebook'] = 'Facebook'; //Add Facebook $contactmethods['linkedin'] = 'LinkedIn'; return $contactmethods; } add_filter('user_contactmethods','social_contactmethods',10,1);
This will provide you controls on User Profile page where you can add/update the links to these social channels.
Step 2: Update your page template with below code to use the information on public website:
/*********************************************** the_author_meta('facebook'); the_author_meta('twitter'); the_author_meta('linkedin'); ***********************************************/ //Above PHP code can be use to construct below HTML component <ul id="socialProfile"> <?php if (get_the_author_meta('facebook')) { ?> <li class="fb"><a href="<?php the_author_meta('facebook'); ?>" title="View user profile on Facebook" target="_blank">Facebook</a></li> <?php } ?> <?php if (get_the_author_meta('twitter')) { ?> <li class="tw"><a href="<?php the_author_meta('twitter'); ?>" title="View user profile on Twitter" target="_blank">Twitter</a></li> <?php } ?> <?php if (get_the_author_meta('linkedin')) { ?> <li class="ln"><a href="<?php the_author_meta('linkedin'); ?>" title="View user profile on LinkedIn" target="_blank">LinkedIn</a></li> <?php } ?> </ul>
Customize WordPress homepage to show page contents as well as blog simultaneously
WordPress allows option either to have blog posts on home screen or a static page (without blog posts) as your homepage. But, in case you want both the options on homepage itself, open home.php and use below code:
<?php if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post(); $page_for_posts_id = get_option('page_for_posts'); setup_postdata(get_page($page_for_posts_id)); ?> <div class="homeContent" id="post-<?php the_ID(); ?>"> <?php the_content(); ?>< </div> <?php rewind_posts(); endif; ?>
Know result count of Search query
<?php $allsearch = new WP_Query("s=$s&showposts=-1"); $key = esc_html($s); $count = $allsearch->post_count; wp_reset_query(); ?>
Modify Excerpt Length and More Tags
Open your functions.php file and add the following codes in there:
// Changing excerpt length function new_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'new_excerpt_length'); // Changing excerpt more function new_excerpt_more($more) { return '…'; } add_filter('excerpt_more', 'new_excerpt_more');
Add Page/Post Slug as Body ID
This method can prove to be critical solution where specific behavior needs to be implemented on a particular page using same shared template. All JS & CSS based implementation can be done based on that ID (i.e. page slug).
<body <?php body_class(); ?> id="<?php echo get_query_var('name'); ?>">
Import Content of Another Page into Template
The approach provides very useful where a section is used across various pages and so we can maintain that section as separate page and import it in the page template. This way any change to the sectioned page is reflected throughout website and we won’t need to make changes everywhere.
<?php //Replace "page-slug" below with the actual page slug with needs to be imported on that template $page = get_posts( array( 'name' => 'page-slug','post_type' => 'page' ) ); if ( $page ){ echo $page[0]->post_content; } ?>
Limit Search and Category Result Count
Add below code to functions.php file under template folder:
function limit_posts_per_archive_page() { if ( is_category() ) $limit = 8; elseif ( is_search() ) $limit = 10; else $limit = get_option('posts_per_page'); set_query_var('posts_per_archive_page', $limit); } add_filter('pre_get_posts', 'limit_posts_per_archive_page');
Redirect to Post if Search Result Has Only One Match
Add below code to functions.php file under template folder:
function redirect_single_post() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1) { wp_redirect(get_permalink($wp_query->posts[0]->ID)); exit; } } } add_action('template_redirect', 'redirect_single_post');
Enjoy playing with WordPress!!