I've created a function in js/custom.js.
Basically, it aligns posts for me in wordpress by adding a class of articleAlign, which will insert content to create a greater gap between the title of the article and the excerpt beneath, this is only necessary when the title of the article does not exceed one line, because for titles that take up two lines, it will push the excerpt of the article down. I want a bigger gap between article titles of one line and the excerpt below them, so that the top of the excerpts are all aligned.
CSS
.articleAlign::after {
display:block;
height:55px;
content:"";
}
jQuery
(function($) {
function articleAlign() {
titleLength = $('.entry-title').children('a').text().length;
if(titleLength < 44) {
$('.entry-title').addClass('articleAlign');
}
}
articleAlign();
})(jQuery);
And of course, here is my enqueue:
wp_enqueue_script('custom-js', get_template_directory_uri() .
'/js/custom.js', array('jquery'), '1.0.0', true);
Note that I'm passing true for $in_footer so the script is just before the </body> tag. (All on one line in original, in the Wordpress functions.php right beneath the enqueue for js/functions.js.)
Why isn't my class getting added?
articleAlign, then step through and examine what's going on in the code as it rights -- does it find the element, does it get the text you expect, what's the length of that text, etc. There's no substitute for debugging.titleLength.textis an odd duck: Unlike all of jQuery's other getters, which only return the relevant value for the first entry in the set, it returns a concatenated string of the text for all the entries in the set. So one possible issue is that there is more than one.entry-titleand you're getting more text than you expect. I picked a Wordpress site at random and found it had two.entry-titleelements, each with the title, rather than one. So perhaps that's it. (But that site's.entry-titleelements didn't have anyain them, so clearly a different design...)