Blog Post Author Link

Description

  • change blog post author link to custom page url and show corresponding posts belong author on this new page
  • view demo – password: abc
  • buy me a coffee

07.26c01v2 Blog Post Author Link

#1. Install Code

#1.1. First, click on Author Name in Blog Post

07.26c01v2 Blog Post Author Link

#1.2. Copy Author ID from Post

In my example, we will have URL:

https://tuanphan-demo.squarespace.com/demo-blog-1?author=5cf999834aa572000105d795

Author ID is: 5cf999834aa572000105d795

07.26c01v2 Blog Post Author Link

#1.3. Create some pages in Not Linked (to host author posts). In my example, we will have 2 pages: /tuan-phan & /khung-long

07.26c01v2 Blog Post Author Link

Next, on these pages, we will use Summary Block + Author Filter code to show posts belong corresponding author. You can follow this free plugin.

07.26c01v2 Blog Post Author Link

#1.4. Click Gear icon on Blog Page

06.26c29v9 Blog Post Print Text

#1.5. Click Advanced > Post Blog Item Code Injection

06.26c29v9 Blog Post Print Text

#1.6. Paste this code

  • If your site is Personal/Basic Plan and it doesn’t support Injection, see #3.1
<!-- 07.26c01v2 Blog Post Author Link -->
<script>
window.BlogAuthorLinkConfig = {
  openInNewTab: false,
  selectors: [
    '.blog-item-author-profile-wrapper a[href*="author="]', '.blog-meta-item--author a[href*="author"]'
  ],

  links: [
    { 
      authorId: '5cf999834aa572000105d795', 
      newUrl: '/tuan-phan?noredirect' 
    },
    { 
      authorId: '659b9a40e5ccb247479f196a',     
      newUrl: '/khung-long?noredirect' 
    }
  ]
};
</script>
<script>
(function () {
  'use strict';

  var CONFIG = window.BlogAuthorLinkConfig;
  if (!CONFIG || !CONFIG.links || !CONFIG.links.length) return;
  if (!CONFIG.selectors || !CONFIG.selectors.length) return;

  function getAuthorId(href) {
    var match = href.match(/[?&]author=([^&]+)/);
    return match ? match[1] : null;
  }

  function applyLinks() {
    var selector = CONFIG.selectors.join(',');
    var anchors = document.querySelectorAll(selector);

    for (var i = 0; i < anchors.length; i++) {
      var a = anchors[i];
      if (a.dataset.balDone) continue;

      var href = a.getAttribute('href');
      if (!href) continue;

      var id = getAuthorId(href);
      if (!id) continue;

      for (var j = 0; j < CONFIG.links.length; j++) {
        if (CONFIG.links[j].authorId === id) {
          a.setAttribute('href', CONFIG.links[j].newUrl);

          if (CONFIG.openInNewTab) {
            a.setAttribute('target', '_blank');
            a.setAttribute('rel', 'noopener noreferrer');
          } else {
            a.removeAttribute('target');
          }

          a.dataset.balDone = 'true';
          break;
        }
      }
    }
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', applyLinks);
  } else {
    applyLinks();
  }

  window.addEventListener('mercury:load', applyLinks);
})();
</script>

07.26c01v2 Blog Post Author Link

#1.7. Update Author ID + New Author Page URL in the code (Line 09 to Line 18)

links: [
   { 
     authorId: '5cf999834aa572000105d795', 
     newUrl: '/tuan-phan?noredirect' 
   },
   { 
     authorId: '659b9a40e5ccb247479f196a',     
     newUrl: '/khung-long?noredirect' 
   }
 ]

07.26c01v2 Blog Post Author Link

#2. Customize

nothing now

#3. Other

#3.1. If you use Personal/Basic Plan and your plan doesn’t support Injection, you can edit Site Footer

05.26c08v1 Event Default Location

Add a Markdown Block

04.26c10v2 Header Sound Icon

Add this code into Markdown

<script>
window.BlogAuthorLinkConfig = {
  openInNewTab: false,
  selectors: [
    '.blog-item-author-profile-wrapper a[href*="author="]', '.blog-meta-item--author a[href*="author"]'
  ],

  links: [
    { 
      authorId: '5cf999834aa572000105d795', 
      newUrl: '/tuan-phan?noredirect' 
    },
    { 
      authorId: '659b9a40e5ccb247479f196a',     
      newUrl: '/khung-long?noredirect' 
    }
  ]
};
</script>
<script>
(function () {
  'use strict';

  var CONFIG = window.BlogAuthorLinkConfig;
  if (!CONFIG || !CONFIG.links || !CONFIG.links.length) return;
  if (!CONFIG.selectors || !CONFIG.selectors.length) return;

  function getAuthorId(href) {
    var match = href.match(/[?&]author=([^&]+)/);
    return match ? match[1] : null;
  }

  function applyLinks() {
    var selector = CONFIG.selectors.join(',');
    var anchors = document.querySelectorAll(selector);

    for (var i = 0; i < anchors.length; i++) {
      var a = anchors[i];
      if (a.dataset.balDone) continue;

      var href = a.getAttribute('href');
      if (!href) continue;

      var id = getAuthorId(href);
      if (!id) continue;

      for (var j = 0; j < CONFIG.links.length; j++) {
        if (CONFIG.links[j].authorId === id) {
          a.setAttribute('href', CONFIG.links[j].newUrl);

          if (CONFIG.openInNewTab) {
            a.setAttribute('target', '_blank');
            a.setAttribute('rel', 'noopener noreferrer');
          } else {
            a.removeAttribute('target');
          }

          a.dataset.balDone = 'true';
          break;
        }
      }
    }
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', applyLinks);
  } else {
    applyLinks();
  }

  window.addEventListener('mercury:load', applyLinks);
})();
</script>

07.26c01v2 Blog Post Author Link

Buy me a coffee