Tuesday, February 21, 2023

Adding a Recent Posts Feature to Your Blog


If you're looking to spruce up your blog, adding a recent posts feature is a great way to do it. This feature will make it easier for readers to find the latest content on your blog. Plus, it can also help keep visitors engaged and coming back for more.



In this article, we'll discuss why adding a recent posts feature is important for your blog and how to do it using code.
Why Add a Recent Posts Feature?

Adding a recent posts feature to your blog is beneficial in a few ways. First, it makes it easier for readers to view the latest content on your blog. Instead of having to scroll through the archives, they can just click on the recent posts link and be taken right to the most current content.

In addition, having a recent posts feature helps keep readers engaged and coming back for more. This can help increase your blog's page views and the amount of time readers spend on your blog.
How to Add a Recent Posts Feature Using Code

Adding a recent posts feature to your blog is a fairly straightforward process. Here's the code you'll need to add it to your blog.
<div>
  <h2>Recent Posts</h2>
  <ul>
    <?php
      $args = array(
          'posts_per_page' => 5,
        'orderby' => 'date',
        'order' => 'DESC'
      );
      $recent_posts = get_posts($args);
      foreach($recent_posts as $post) { ?>
        <li><a href="<?php the_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a></li>
      <?php } ?>
  </ul>
</div>

This code will create a list of your five most recent blog posts which you can place anywhere on your blog. Simply copy and paste the code into the theme file of your choice, then customize it to your liking.
Conclusion

Adding a recent posts feature to your blog is an easy way to keep readers engaged and coming back for more. Plus, it makes it easier for visitors to find.

You can also try the code below. Good luck!

<div class="recentpoststyle">
<script type="text/javascript">
function showlatestposts(e){for(var t=0;t<posts_no;t++){var r,s=e.feed.entry[t],n=s.title.$t;if(t==e.feed.entry.length)break;for(var a=0;a<s.link.length;a++)if("alternate"==s.link[a].rel){r=s.link[a].href;break}n=n.link(r);var i="... read more";i=i.link(r);var l=s.published.$t,o=l.substring(0,4),u=l.substring(5,7),c=l.substring(8,10),m=new Array;if(m[1]="Jan",m[2]="Feb",m[3]="Mar",m[4]="Apr",m[5]="May",m[6]="Jun",m[7]="Jul",m[8]="Aug",m[9]="Sep",m[10]="Oct",m[11]="Nov",m[12]="Dec","content"in s)var d=s.content.$t;else if("summary"in s)var d=s.summary.$t;else var d="";var v=/<\S[^>]*>/g;if(d=d.replace(v,""),document.write('<li class="recent-post-title">'),document.write(n),document.write('</li><div class="recent-post-summ">'),1==post_summary)if(d.length<summary_chars)document.write(d);else{d=d.substring(0,summary_chars);var f=d.lastIndexOf(" ");d=d.substring(0,f),document.write(d+" "+i)}document.write("</div>"),1==posts_date&&document.write('<div class="post-date">'+m[parseInt(u,10)]+" "+c+" "+o+"</div>")}}
</script>
<script type="text/javascript">
var posts_no = 5;var posts_date = true;var post_summary = true;var summary_chars = 80;</script>
<script src="/feeds/posts/default?orderby=published&amp;alt=json-in-script&amp;callback=showlatestposts">
</script><a style="font-size: 9px; color: #CECECE;margin-top:10px;" href="https://1001thuthuat.blogspot.com/" rel="nofollow">Recent Posts Widget</a>
<noscript>Your browser does not support JavaScript!</noscript>
<style type="text/css">
.recentpoststyle {counter-reset: countposts;list-style-type: none;}
.recentpoststyle a {text-decoration: none;color: #49A8D1;}
.recentpoststyle a:hover {color: #000;}
.recentpoststyle li:before {content: counter(countposts,decimal);counter-increment: countposts;float: left;z-index: 1;position:relative;font-size: 15px;font-weight: bold;color:#fff;background:##69B7E2; margin:13px 5px 0px -6px;line-height:30px;width:30px;height:30px;text-align:center;-webkit-border-radius:50%;-moz-border-radius:50%;border-radius:50%;}li.recent-post-title{margin-bottom: 5px;padding: 0;}
.recent-post-title a {color: #444;text-decoration: none;font: bold 13px "Avant Garde",Avantgarde,"Century Gothic",CenturyGothic,AppleGothic,sans-serif;}
.post-date {font-size: 11px;color: #999;margin:5px 0px 15px 32px;}
.recent-post-summ {border-left:1px solid #69B7E2; color: #777; padding: 0px 5px 0px 20px; margin-left: 10px; font: 15px Garamond,Baskerville,"Baskerville Old Face","Hoefler Text","Times New Roman",serif;}
</style></div>

0 comments:

Post a Comment

Please leave your comment here. Làm ơn ghi rõ nguồn khi lấy thông tin từ INFO TECH.

» Có thể sử dụng các tài khoản tại Blogger-Google, LiveJournal, WordPress, TypePad, AIM, OpenID, hoặc sử dụng Tên/URL-Website [có thể bỏ trống URL], hoặc Ẩn danh để đưa ra comemnt - nhận xét, nếu muốn.

» Đặc biệt - có thể đăng trực tiếp ảnh, mp3, video [của youtube] bằng cách copy - paste rõ ràng link trực tiếp của ảnh [.jpg, .gif, .png], mp3 [.mp3], video [từ youtube.com] vào comment form.

» Ngoài ra có thể bấm vào Preview bên dưới comment form để xem trước conment đã viết, trước khi post-đăng. Tương tự, bấm vào Subscribe by email để đăng ký theo dõi comments của bài này.

» Vui lòng đăng những nhận xét lịch sự và cố gắng gõ tiếng Việt có dấu, đúng chính tả nếu có thể. Rất vui vì những comment thiện ý, thanks!

Latest Posts

Label tag

Page copy protected against web site content infringement by Copyscape
 
About Me
Info Tech provies IT tips, Applications, Blogger, Blog, Adsense ... Use Firefox to open this site!