Count and display most recent blog posts in javascript

What is the javascript in order to only display posts 3 & 4 in order??? Also I need it be dynamic so if I put a 5th post it will only display 4th and 5th posts... I was thinking about something like a date function or a simple incrementor but can't seem to figure it out. I'm new to javascript and have been trying different things but no avail... Thanks in advance...

 <!DOCTYPE html>
<html>
<body>

<div id="posts-div">
    <h1 class="post-title">post4</h1>
    <p class="post">post4</p>
</div>

<div id="posts-div">
    <h1 class="post-title">post3</h1>
    <p class="post">post3</p>
</div>

<div id="posts-div">
    <h1 class="post-title">post2</h1>
    <p class="post">post2</p>
</div>

<div id="posts-div">
    <h1 class="post-title">post1</h1>
    <p class="post">post1</p>
</div>

<script>
// ???
</script>

</body>
</html>

You dont need script for that. You can do it with CSS.. I have changed your html little bit (made posts-div class in html).

.posts-div{
  display:none;
}
.posts-div:nth-child(-n+2) {
   display:block;
}
<!DOCTYPE html>
<html>
<body>
  <div class="posts-div">
      <h1 class="post-title">post5</h1>
      <p class="post">post5</p>
  </div>
  <div class="posts-div">
      <h1 class="post-title">post4</h1>
      <p class="post">post4</p>
  </div>

  <div class="posts-div">
      <h1 class="post-title">post3</h1>
      <p class="post">post3</p>
  </div>

  <div class="posts-div">
      <h1 class="post-title">post2</h1>
      <p class="post">post2</p>
  </div>

  <div class="posts-div">
      <h1 class="post-title">post1</h1>
      <p class="post">post1</p>
  </div>

<script>
// ???
</script>

</body>
</html>

Recent Blog Posts By Ben Nadel, Ben Nadel's web development blog on ColdFusion, jQuery, HTML5, AJAX, SQL, and all aspects of On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love. Recent Blog Posts by Ben Nadel lowered the opacity on the rectangle in order to allow the highlighted text to show-thr read more »  The recently published posts to your blog can be displayed on your website using the blog's feed in conjunction with javascript. When you publish a new post to your blog, the recent posts list on your website will automatically update to display the latest posts with the javascript added to your website. First, grab the URL for your blog's feed. The feed for your blog can be found by adding atom.xml to end of your blog's URL or clicking the Subscribe link on your blog. For example:

$('.posts-div:gt(1)').hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>

<div class="posts-div">
    <h1 class="post-title">post4</h1>
    <p class="post">post4</p>
</div>

<div class="posts-div">
    <h1 class="post-title">post3</h1>
    <p class="post">post3</p>
</div>

<div class="posts-div">
    <h1 class="post-title">post2</h1>
    <p class="post">post2</p>
</div>

<div class="posts-div">
    <h1 class="post-title">post1</h1>
    <p class="post">post1</p>
</div>

</body>

Add Disqus Comment Count to your Static Blog Index Page , This shows you how to display the number of Disqus comments for you static blog posts on the home page or any other article extracts/list page  If the most recent posts are somewhat linked to each other, then it might engage your visitors for a bit longer. However, in some designing methods people want to display recent posts differently according to their design.

Try this:

<script>
document.addEventListener("DOMContentLoaded", function () {
    var allPosts = document.querySelectorAll(".posts-div");
    // This is the number of posts you want displayed
    var numberOfPostsToShow = 2;

    for (var i = 0; i < allPosts.length; i++) {

       if(i > numberOfPostsToShow - 1) {
           allPosts[i].style.display = "none";
       }

    }
});
</script>

This way you will choose how many posts you want to be shown with the numberOfPostsToShow variable.

Let me know if this worked. Regards.

Smashing WordPress: Beyond the Blog, Show. off. Your. Twitter. There is certainly no doubt that the rise of Twitter has a limitation to someone used to punching out 3,000-character blog posts, and most stylistic ways to show off your Twitter updates is to display the latest 288 tweet. the Twitter API (see the reference at apiwiki.twitter.com) and some JavaScript. One of the most common client requests is to display the most current blog posts on their front page. So, for this tutorial, we will go over how to take the four most recent blog posts and display them horizontally across the front page. We’ll also be using a static front page, and working in the “front-page.php” page template. Define CSS

Display Instagram Posts on a Webpage with Instafeed.js, Display Instagram Posts on a Webpage with Instafeed.js This article has been updated to include a method to show 'user details' in each post. respective URLs from my IG account, I also wanted to show the Like count and Comment count to go with each image. Home About Services Portfolio Blog. #5 Display Recent Posts by Category on Blog Page. In addition to the main blog feed on the blog page template, you can also display your recent posts by category. This is a great way to organize blog page content by breaking up the layout a bit and highlighting different categories at the same time. Here’s how to do it.

How to easily display a twitter feed using javascript, If you remove the count parameter you get back the most recent 15 status updates. You can use the same URL to get the timeline of the account  Put the following in the sidebar or wherever you like the recent posts to list.. You can give it 2 arguments, the first is the number of posts and the second is whether or not you want to display the excerpts. so recent_posts(2, false) will display the 2 most recent post titles.

Load More Posts with AJAX in WordPress with no plugins., Two ways of loading new WordPress posts asynchronously - with the load more works for you // don't display the button if there are not enough posts if '/​myloadmore.js', array('jquery') ); // now the most interesting part // we  This is a simple widget. This widget comes with the counter. That means your most recent post will be ranked number 4 in this widget. It will show the post title and a small snippet right below the title. The user will be able to see the post published date and read more link too. No thumbnail is available in this widget.

Display count down in WebClient UI, Create a new WebUI component with a view main.htm: Paste the lwtCountdown-1.0.js"></script> <link rel="Stylesheet" type="text/css"  I have added a Content Query web part, and managed to get the links to the blog posts to display, but I Stack Exchange Network Stack Exchange network consists of 176 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Comments
  • You might want to change your code: you're listing different elements with the same id, which is invalid. You probably want to use a class instead.
  • be dynamic so if I put a 5th post : put it were ?, can you put any code you tried before ? are you using JQuery or it's pure js ?
  • Also, it might be a good idea if you organise your divs inside another div, this time identified with an id. Then you can use appendChild() to add new posts and you can use a MutationObserver to detect any changes in the container and the child divs. But then, why should you complicate your life with a MutationObserver at all, when you can manage which elements are visible every time you add a post?