How to Add Read More to WordPress Articles in Home


This post is only for new users in the WordPress field due to the many questions about it and also the difficulty of implementing it.

We will discuss useful solutions that will spare everyone from falling into hard work to add Read more in the normal way to each post separately. The important thing is to explain the normal method and then move to the new method using the hack

Notes

You must know what are the functions that control the appearance of content on the home page, archive, classifications, and the difference between them. To sum up, there are two types:

the_content : This function displays the entire content, meaning texts, images, ads, video, that is, everything contained in the post. It is often used in the single post and pages.

the_excerpt : As for this function, it displays a text excerpt only, and the maximum, I think, is 55 words that are displayed and are often used in archives, home pages and categories.

This lesson applies only to templates that use the_content and the_excerpt functions. As for the rest of the templates, such as arras, which use their own functions to display content, for example, the following function arras_above_content does not apply to the explanation

How do I know which functions to use the template?

The matter is very simple. All you have to do is open either the index.php file and search in the file for the_excerpt function. Let’s say we have the alltuts template. After searching, you will find the following:

<div class=”textPreview”>
<?php the_excerpt(); ?>
</div>

view rawread-more-the-excerpt.html hosted with ❤ by GitHub

Or in other templates you may find the following:

1<div class=”textPreview”>
2<?php the_content(‘More …’); ?>
3</div>

view rawread-more-the-content.html hosted with ❤ by GitHub

In the case of the alltuts template, it displays a snippet, but in the case of the opposite, you will find the template uses the_content function, I hope that the matter is clear

Read more – the normal way

Here we will use a button located in the text editor bar to do this process. All you have to do is press the button as in the image below

wordpress editor continue button - WordPress Magazine

The word “Read More” will appear for you as in the image, and here you should know that only the text from the beginning of the post to the Read More will appear on the main page, but if the template uses the_excerpt, it will display only 55 words

Read more – Hack

Here we will use a special function of ours or the so-called hack. One of the advantages of this function is that you will not have to modify all posts. Once you add the function, the settings will apply to all posts without exception.

The important thing is this function controls the maximum number that you want to display from the post in the main or anywhere you want to display an excerpt from the post without prolonging. We move on to the hack that will do the work for us, which is as follows:

Read more – How to use the hack

The method is easy. All you have to do is open the template’s functions.php file and add the code below at the end of the file so that the php error does not fall into the template. If you encounter a problem, you may add the code after locking the php tag

1/*
2* get content and clean
3*/
4function mwp_post_content($post_id) {
5$post = get_post($post_id);
6$output = $post->post_content;
7$replace = “@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@”;
8$output = wp_strip_all_tags($output, true);
9$output = strip_shortcodes($output);
10$output = ltrim($output);
11$output = trim($output);
12$output = preg_replace($replace, ”, $output);
13return $output;
14}
15/*
16* Content Word Limit
17*/
18function content($limit, $post_id) {
19$data = mwp_post_content($post_id);
20$content = explode(‘ ‘, $data, $limit);
21if (count($content) >= $limit) {
22array_pop($content);
23$content = implode(” “,$content).’…’;
24} else {
25$content = implode(” “,$content);
26}
27return $content;
28}

view rawread-more-functions.php hosted with ❤ by GitHub

If the template does not have a properties file, then create a new one and add the hack inside it. Do not forget to add the code at the end of the file and then save the file and upload it to the template folder

Now go to index.php and search for the_content or the_excerpt function and replace it with the following code:

1<div class=”textPreview”>
2<?php echo content(30, $post->ID); ?>
3</div>

view rawread-more-limit-example.html hosted with ❤ by GitHub

You can change the value 30 to the number you want depending on the length of the text that you want to appear in the main and this length will apply to all previous posts. You can use this method for archives and categories, tags from the files they are responsible for

Read more – Suggested additions

There are many plugins for those who like to use them instead of modifying the template on the official WordPress site WordPress.org, you will find them in the plugins category, and some of them do the same work instead of using the hack

  1. My Read More
  2. Read More Online
  3. Read More Right Here
  4. Slide Read More
  5. AJAX Read More
  6. Custom More Link Complete
  7. Auto Read More Generator

I have not tried these add-ons because I prefer to use the hacks due to the many problems with the add-ons and the ease of dealing with the hack

end of transmitter

This is all there is to it, and the method, as you can see, is easy. Using the hack is better than using the normal method if the articles are many. In the end, I hope the explanation is clear.

Peace, mercy and blessings of God

Leave a Comment