Product title coding to place articles like "A", "An" or "The" at the end of the title

Hi,

I’m developing on an online bookshop: https://70087b-2.myshopify.com/collections/all

I’d like the books to be titled like this:
“Grandmother Begins the Story, A”
“Secret Hours, The”

Instead of:
“A Grandmother Begins the Story”
“The Secret Hours”

Since my shop uses a plugin called Biblioshare that automatically pulls book titles from a national database, and the database is constantly refreshed, I cannot simply rename the book titles the way I want to as they will revert to the database entry. I’m wondering if someone might have a piece of code I can use in my theme so that when titles are pulled from the database, they are automatically changed to have articles like “A”, “An” or “The” placed at the end of the title, after a comma.

Many thanks!

Hi @mparsons ! Can I ask you why it is important for your bookstore?

Thank you.

Thanks for your question, Cristina. Alphabetization that ignores articles at the beginning of titles is part of standard APA style, and is widely used in library cataloguing. We want to match this style in our bookstore for that reason. Plus, it makes finding book titles much easier!

UPDATE: Shopify support recommends engaging a Shopify Partner to write the code.

ChatGPT suggests this, which so far does not work. Any idea how to adjust it to work?

$(document).ready(function() {
// Wait for the titles to be loaded (you might need to adjust the selector)
setTimeout(function() {
rearrangeTitles();
}, 1000); // Adjust the delay based on your site's loading time
});

function rearrangeTitles() {
// Change the selector to match your product title elements
$('.product__title').each(function() {
var title = $(this).text();
var articleMatch = title.match(/^(A|An|The)\s(.+)$/);

if (articleMatch) {
var newTitle = articleMatch[2] + ', ' + articleMatch[1];
$(this).text(newTitle);
}
});
}

Thank you @mparsons for the information!