Customize the WordPress related post display plugin Yet Another Related Posts Plugin (YARPP) to be responsive.

update Last updated: December 16, 2023 at 4:53 PM

When displaying related articles on a blog, the number of lines displayed on a smartphone was quite large and it was quite difficult to see, so I made it responsive by customizing the WordPress plugin ``Yet Another Related Posts Plugin (YARPP)'' that displays the following related articles. I made it compatible with.

I used the following article as a reference when customizing this plugin.
You can decorate related articles with the same number of columns (changes depending on CSS settings) on all devices, such as PCs and smartphones, and display them beautifully.

Customizing PHP code

Get the YARPP template file and customize the PHP code as shown below.

  1. In WordPress settings, select Custom from "YARPP", click "Move Template", select "Thumbnails" and click "Save Changes".
    ⇒ Template file: yarpp-template-thumbnail.php
  2. Rewrite the template file "yarpp-template-thumbnail.php" added to the same level as the theme folder to the code below.
<?php if(have_posts()):?>
     <div class="related-post">
     <?php while(have_posts()) : the_post(); ?>
          <?php if(has_post_thumbnail()):?>
               <div class="related-entry">
                    <div class="related-thumb">
                         <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                         <?php the_post_thumbnail("thumbnail"); ?>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                    /* タイトルの表示制限30文字 */
                         if(mb_strlen($post->post_title, 'UTF-8')>30){
                              $title= mb_substr($post->post_title, 0, 30, 'UTF-8');
                              echo $title.'';
                         echo $post->post_title;
          <?php endif; ?>
     <?php endwhile; ?>
<?php else: ?>
<?php endif; ?>

In the function “the_post_thumbnail” that outputs the marked eye-catching image, you can also specify other thumbnails with different aspect ratios (such as ‘post-thumbnail’) if you like.

Add CSS code

The following CSS code has been added to this site (enter "by Senri" in any parts that have been modified from the original). The following description corresponds to the theme of the homepage builder (HPB22).
In a 4-column display like this site, images will look grainy if the thumbnail size is less than 150px when accessed from a PC, so change the thumbnail size to a value greater than the default 150px in the WordPress media settings. is recommended.
If you want to set the thumbnail to a 4:3 aspect ratio instead of square, set it to 150 (width) x 112 (height) in the media settings.
Please note that if you change the thumbnail size, you will need to regenerate the media file using the plugin "Regenerate Thumbnails".

.related-entry {
	vertical-align: top;
	width:22%;/*②1記事の領域(幅)by Senri */
	font-size: 12px;/*文字サイズ by Senri */
	line-height: 1.4;/*文字行高*/
	margin-bottom: 12px;/*下余白*/
	padding: 0 1.3%;/*記事の間隔(左右)by Senri */
.related-entry img{
 	border: #ccc 1px solid;/*画像の枠線*/
	height: auto; /* by Senri */
.related-thumb a,
.related-thumb a img{
	-webkit-box-shadow: none;
	box-shadow: none;
	text-decoration: none;
	border-radius: 10%; /*角丸 by Senri */
/*マウスオーバーで光る by Senri */
.related-thumb a img:hover{	
	-webkit-filter: brightness(1.3);
	-moz-filter: brightness(1.3);
	-o-filter: brightness(1.3);
	-ms-filter: brightness(1.3);
	filter: brightness(1.3);
  メディアクエリ(スマホ対応)by Senri
@media (max-width:480px) {
	.related-entry {
		/* YARPP の文字サイズを変更 */
		font-size: 9px;

Lines 37 to 44 of the above code "Light up on mouseover" use the filter property brightness function filter that makes the thumbnail image glow white on rollover even if the background is black.
However, this description is compatible with Chrome, Safari, Opera, and Edge, but not IE. If you want to make it compatible with IE, please replace it with the opacity version written in the original below (currently, support for IE has ended, so there is no need to do so in reality). In that case, if the background is black, the thumbnail image will not shine white during rollover, but on the contrary, the thumbnail image will darken.

.related-thumb a:hover{
	-ms-filter: “alpha( opacity=70 )”;
	-webkit-box-shadow: none;
	box-shadow: none;
	text-decoration: none;

Line 35 of the above code is the border-radius property for rounding the four corners, but this is specified when the thumbnail image is square. If the thumbnail image is not square, please specify the absolute value instead of the relative value as shown below. If you don't want the corners to be rounded, remove this line.

border-radius: 10px;

The code from line 45 onwards is compatible with smartphones (screen width 480px or less). In this example, the font size of YARPP is set to 9px when accessed from a smartphone.
If there are other settings for smartphone operation, please add them to this section.

Add this entry to the hasebookmark
X (post)

Leave a Reply