I use a custom post on WordPress, I had an issue with the post date not displaying by default on the archive page
so I added codex code to displaying it
it worked fine but the problem is each post date displaying before the element
for example:
<!-- Post 1-->
<div class=".archive-custom-post-date">02/03/2021</div>
<article>Post 1</article>
<!-- Post 2-->
<div class=".archive-custom-post-date">02/03/2021</div>
<article>Post 2</article>
<!-- Post 3-->
<div class=".archive-custom-post-date">02/02/2021</div>
<article>Post 3</article>
<!-- Post 4-->
<div class=".archive-custom-post-date">0`/03/2021</div>
<article>Post 4</article>
<!-- Post 5-->
<div class=".archive-custom-post-date">12/02/2021</div>
<article>Post 5</article>
so I tried to add jQuery code to iterate over each post date and append them to each post but it didn’t work, they attach to the last post (all dates) only, or adding all post dates to each post for example each post will have all dates.
//@1 if I use this function it will add all post dates to each post
function append_post_date() {
jQuery(".archive .archive-custom-post-date").each(function (
idx,
date_element
) {
return jQuery(date_element).appendTo(".archive article");
});
}
//@2 if I use this function it will added all post dates to last element on dom
function append_post_date() {
jQuery(".archive .archive-custom-post-date").each(function (
idx,
date_element
) {
// #2
jQuery(".archive article").each(function (idx, article_element) {
return jQuery(this).append(date_element);
});
});
//end
}
2
Answers
You can loop of
'.archive-custom-post-date'
each and append to the nextarticle
. check below snippet.Solution in vanilla js