abdulkrm

Set H1 as og:title

Jan 22nd, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. add_action( 'wp_head', function() {
  2. if ( is_product_category() ) {
  3. ?>
  4. <script type="text/javascript">
  5. document.addEventListener('DOMContentLoaded', function() {
  6. // Find the og:title meta tag
  7. var ogTitleContent = document.querySelector('meta[property="og:title"]')?.getAttribute('content');
  8. if (ogTitleContent) {
  9. // Split the content at the separator
  10. var titleParts = ogTitleContent.split(' - ');
  11. if (titleParts.length > 0) {
  12. // Set the first part as the new h1 content
  13. var newTitle = titleParts[0];
  14. var h1 = document.querySelector('.woocommerce-products-header__title.page-title');
  15. if (h1) {
  16. h1.textContent = newTitle;
  17. }
  18. }
  19. }
  20. });
  21. </script>
  22. <?php
  23. }
  24. });
Advertisement
Add Comment
Please, Sign In to add comment