How to Display Related Posts in WordPress Without Plugins
WordPress is one of the most popular engines and plugins are written for it for every taste and for almost any task.. I think everyone, who developed a site that has a lot of content thought about how to get users to stay on the site and read articles further. To keep users on the site, it is necessary for them to offer articles of interest to them. The plugin solves this problem perfectly related post thumbnails but this plugin has a lot of settings, which are not always needed., and if you already have a lot of plugins installed, then adding a few more can really slow down your site. It was for these reasons that I began to dig on the Internet and look for a solution to the problem of displaying similar blog posts using simple code., no extra settings.
One of the reasons for writing this article was that, that the plugin that I used to use had a conflict in working with another plugin and displayed instead of four pictures 16. All in all, the story was not good.
Another reason why you need to be able to write plugins yourself, is the creation of premium themes and selling them on the Internet on sites such as themeforest.net or for your clients. One of the most important requirements of themeforest when developing a theme for a site is its support and development.. If the plugin you were using stopped working, then explain to those who have already bought your theme, why don't they work, won't lead to anything good. If you wrote your plugin for example, similar articles by all rules WordPress API and embed it in your theme, it is unlikely that it will be necessary to deal with improvements in the future and just like that it will not stop working.
And now, in fact, about similar entries with thumbnails
Paste the code on the page single.php in the right place.
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 |
<?php function my_related_posts() { $tags = the_tags(''); $args = array( 'posts_per_page' => 4, 'post__in' => $tags, ); $the_query = new WP_Query( $args ); echo '<ul>'; while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail(); } ?> </a><br clear="all" /></li> <?php endwhile; echo '</ul>'; wp_reset_postdata(); } ?> <?php my_related_posts() ?> |
After inserting the code, similar articles and pictures of different widths will be displayed on your page. It remains only to correct the css styles.
Making a plugin based on code
Now if you need a plugin, you can make it from all this, to use it on your projects. First, in the plugins folder, create a folder my-related-post-thumbnail and add a file to it related-post-thumbnail.php. In the file you need to place the code of the created 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php /** * @package Package name * @version 1.0 */ /* Plugin Name: My plugin my-related-post-humbnail Plugin URI: https://wp-admin.com.ua Description: спасибо сайту wp-admin.com.ua, за хороший пример плагина ) Armstrong: My Plugin. Author: Name Author Version: 1.0 Author URI: https://wp-admin.com.ua */ /* display related data: <?php echo my_related_posts(); ?> */ function my_related_posts() { $tags = the_tags(''); $args = array( 'posts_per_page' => 4, 'post__in' => $tags, ); $the_query = new WP_Query( $args ); $res .= '<ul>'; while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail( ); } ?> </a><br clear="all" /></li> <?php endwhile; $res .= '</ul>'; wp_reset_postdata(); return $res; } ?> |
To insert code into a template, you need to add code, for example, in single.php, to the right place.
1 2 3 4 5 |
<?php if ( function_exists( 'my_related_posts' ) ) { echo my_related_posts(); } ?> |
That's all for today, good luck with development.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.