0

I am trying to use jquery to automatically insert "Latest Post" tag to every post published "today."

Here's the code I put in my blogger right before </head>:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
<![CDATA[
    var today = new Date();
    var date = today.getMonth() + "/" + today.getDate() + "/" + today.getFullYear();
    var x = document.getElementsByTagName("abbr").innerHTML;
    var NewIn = "\",\"<a href="https://fgzone4us.blogspot.com/search/label/Latest%20Post" rel="tag">Latest Post</a>"
    if (date === x){
        $(span.post-labels).append(NewIn);
    }  
]]> 

</script>

I did some search and tried my best to put together the codes. However, it doesn't work and I don't know which steps are wrong. Hope someone can help me out as I am not very good at coding.

Thanks in advance.


@aax Thanks for the help, I'm still trying, but just doesn't work.

1 Answers1

0

The main ideas on how to make this work:

  1. Use $(document).ready(function() { ... } to manipulate the page. If not used, the page might not have been loaded and the manipulation fails.
  2. Date.getMonth is zero-based (e.g. the month of January is represented by 0 and July is 6. When comparing with the blog post date, you need to add 1 to it.
  3. You need to decide for each blog post if it should have the "latest" tag. So you need some kind of loop which checks the date for each blog post and then adds the tag for this post only. In jQuery, use $(<parent element>).find("<sub element selector>") to select a child element of a specific parent.

I tested the following code on your blog:

$(document).ready(function() {
    var today = new Date();
    var date = (today.getMonth() + 1) + "/" + today.getDate() + "/" + today.getFullYear();
    var newIn = $.parseHTML(', <a href="https://fgzone4us.blogspot.com/search/label/Latest%20Post" rel="tag">Latest Post</a>')

    $("div.post").each(function() {
       if ($(this).find("a.timestamp-link abbr").text() === date) {
           $(this).find("span.post-labels").append(newIn);
       }
    });
});

According to https://www.w3schools.com/tags/tag_script.asp the script tag should generally look like this:

<script type="text/javascript">
//<![CDATA[

...

//]]>
</script>
aax
  • 394
  • 5
  • 10
  • Thanks for your help. But still it doesn't work. This problem really gives me lots of gray hair. – SunMoon Hong Jul 14 '20 at 07:35
  • Can you provide an error message from the browser console? – aax Jul 14 '20 at 07:42
  • the tricky thing is that there is no error message after I pastes your code to my blogger. I just published another new posts, but there is no "Latest Post" tag inserted below. Thank you for working on the problem with me:) – SunMoon Hong Jul 14 '20 at 08:19
  • Actually, there is an error now: https://imgur.com/a/wn9LpXC I removed the CDATA tag from the code in the answer, it seems to cause the issue and is not necessary. – aax Jul 14 '20 at 08:27
  • Oh...Now I kind of know what you meant by error from "browser console." I only looked at elements, that's why I couldn't tell there's something wrong. – SunMoon Hong Jul 14 '20 at 08:32
  • This time, there is an error saying: Open quote is expected for attribute "href" associated with an element type "a" https://photos.app.goo.gl/Q3CbzXMz35W4rbfp8 – SunMoon Hong Jul 14 '20 at 08:39
  • Still can't solve the open quote problem even if I tried the methods provided here https://www.oxygenxml.com/forum/topic5092.html – SunMoon Hong Jul 14 '20 at 08:48
  • Here are some resources that I used to come up with a next attempt: - https://developer.mozilla.org/en-US/docs/Archive/Web/Writing_JavaScript_for_HTML - https://api.jquery.com/Types/#htmlString – aax Jul 14 '20 at 09:10
  • thank you. Although there's no solution by far, I'm really sorry for wasting so much of your time and greatful for your help of trying to work out the problem for me. I've been trying for solving this problem for weeks, and I will still try my best to figure it out with the directions you offered. Many thanks to you! – SunMoon Hong Jul 14 '20 at 09:30
  • Sorry for not testing my suggestions properly. I did that now and rewrote the answer. – aax Jul 14 '20 at 11:11
  • aax, thank you for your help but a new problem arises. It does append "Latest Post" but just to one of my latest posts. And when I click on Latest Post tag, there are no post collections under the tag: shorturl.at/szBS0 – SunMoon Hong Jul 15 '20 at 08:43