How To Have WordPress Pagination In Your Website Without The Use Of Any Plugins
Without any second thought, WP-PageNavi is one such plugin which everyone knows well, which everyone love very much and which everyone wants to get installed in their WordPress website. But, certainly, just like our other snippets tips, for this also, we are having something for you. It’s pretty simple, on copying and pasting the following snippet in your WordPress website, you will be able to have the pagination active and that too without the use of any plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
global $wp_query; $total = $wp_query->max_num_pages; // only bother with the rest if we have more than 1 page! if ( $total > 1 ) { // get the current page if ( !$current_page = get_query_var('paged') ) $current_page = 1; // structure of "format" depends on whether we're using pretty permalinks $format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/'; echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => $format, 'current' => $current_page, 'total' => $total, 'mid_size' => 4, 'type' => 'list' )); } |
Snippet Source/Credit: Smashing Magazine
How To Prevent WordPress To Compress Your JPG Files
By default, WordPress compress upload jpg image files. While it is a good practice but sometimes, you need not such things. To prevent this, all you have to do is to copy and paste the following snippet in your theme’s functions.php file and it will let you prevent WordPress to compress your jpg files.
1 |
add_filter('jpeg_quality', function($arg){return 100;}); |
Snippet Source/Credit: WP Snippets
How To Make Your Search Results Unlimited
By default, WordPress displays ten posts on the search result page while there are some from us, who wish to see unlimited number of search results on the same page. Putting the following snippet in your theme’s search.php, you will be able to do so. Start by finding the below code in your search.php file:
1 2 3 |
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> |
Once you found it, simply replace it with:
1 2 3 4 5 |
<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> |
Snippet Source/Credit: WPHacks
How To Redirect Users To A Random Post
You do often wish that what will happen if there is something by which you will be able to redirect users to a random post. Are you? If yes, then let’s make your job easy here, pasting the following snippet to your page-random.php file will let you redirect users to a random post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// set arguments for get_posts() $args = array( 'numberposts' => 1, 'orderby' => 'rand' ); // get a random post from the database $my_random_post = get_posts ( $args ); // process the database request through a foreach loop foreach ( $my_random_post as $post ) { // redirect the user to the random post wp_redirect ( get_permalink ( $post->ID ) ); exit; } |
Snippet Source/Credit: Smashing Magazine
How To Change The Label In Your WordPress Blog
It is pretty much common that you want to customize the label of certain things, say “post” label to “article” in your WordPress blog. It can be done with just few lines of code, just simply copy and paste the following snippet in your theme’s functions.php file and you are done.
1 2 3 4 5 6 7 8 9 |
add_filter('gettext', 'change_post_to_article'); add_filter('ngettext', 'change_post_to_article'); function change_post_to_article($translated) { $translated = str_ireplace('Post', 'Article', $translated); return $translated; |
Snippet Source/Credit: Smashing Magazine
How To Speed Up Your Blog By Adding Caching Custom Queries
If you are looking onto a method by which you can speed up your WordPress blog, then this is one such which you have to definitely try. All you have to do is add caching custom queries in your blog and you will be able to speed up your website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php // Get any existing copy of our transient data if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' ); set_transient( 'special_query_results', $special_query_results ); } // Use the data like you would have normally... ?> |
Snippet Source/Credit: WordPress Codex
How To Create A Dropdown Menu Of Tags
By default, WordPress offers dropdown menu of categories (through wp_ dropdown_categories() function). But, what if you will be requiring or wish to display your tags in a dropdown manner as well. Does it sound good? Yes, then simply paste the following code at the place where you want the dropdown menu to be displayed. Make sure to modify the code on line 5 as per your requirements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<h2><?php _e('Posts by Tags'); ?></h2> <form action="<?php bloginfo('url'); ?>/" method="get"> <div> <?php $select = wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&show_count=1&orderby=name&echo=0'); $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?> <noscript><div><input type="submit" value="View" /></div></noscript> </div></form> |
Snippet Source/Credit: WPLover
How To Test Whether A Plugin Is Active Or Not
For one-to-many times, you did often considerably think over the context that what it takes exactly from the backend which says that whether a plugin is active or not. Well, if you too found yourself in such a situation and don’t know what solution to opt for, then all you have to do is to simply paste the following code below and it will let you know that whether a specific plugin is active or not.
1 2 3 4 5 6 7 8 9 |
<?php if (is_plugin_active('plugin-directory/plugin-file.php')) { // the plugin is active } ?> |
Snippet Credit/Source: CatsWhoCode