I'm working in Wordpress and trying to add an excerpt to each portfolio card in a grid (there's no way for me to do this via the theme.
Here is the custom script that I am using. Everything works as expected, except that only the last node in the list gets the excerpt. I've changed the number of iterations around to test this and it always just inserts the excerpt on the last node that it iterates through.
Based on this it appears that each new iteration undoes or erases what the prior one did.
window.onload = function() {
const targets = document.querySelectorAll('.entry-header');
const parent = document.createElement('div');
const child = document.createElement('p');
const excerpt = 'This will be the excerpt...';
parent.className = "entry-excerpt";
parent.appendChild(child);
child.innerHTML = excerpt;
for (let i = 0; i < targets.length; i++) {
targets[i].after(parent);
}
};
I logged targets[i] and each iteration prints correctly