🔧 Custom Tailoring: JSON-LD Script for Specific Nodes

Roiarthur

Active member
🔧 Custom Tailoring: JSON-LD Script for Specific Nodes

👋 Introduction

Copy-pasting a generic script onto a production site is like putting a Lada part on a Ferrari. It fits, but it's going to smoke. For a serious board, you need to target your specific Tutorial forums (not the Off-Topic bar or the Trash can) and, more importantly, display your actual branding, not just the default XenForo variables. We are going to turn that rough draft into production-grade code.



🎯 Target Audience

XenForo Admins who want their guides to shine in Google with proper "TechArticle" Rich Snippets.



🧰 Tools or Prerequisites

• List of IDs for your "Tutorial" nodes (e.g., Windows=12, Linux=14, Hardware=20)

• A direct, clean URL to your logo (square or rectangle, PNG/JPG)



🗺️ Overview

Step #1
> Easy > Identifying Node IDs (Arrays)

Step #2 > Intermediate > Customizing the Code (Logo & Brand)



🛠️ Step-by-Step Tutorial

🔹 Step #1 > Multi-Targeting (Array)

1️⃣ Action : Replace the single condition == 10 with an in_array check.

2️⃣ Utility : You likely have multiple tutorial sections. This code must execute on all those sections, but NOT on "General Discussions".

3️⃣ Explanation : We switch from "If it's forum 10" to "If it's forum 10, OR 12, OR 45". Note down your IDs by looking at the URL of your forums in the AdminCP (e.g., /nodes/windows.12/).



🔹 Step #2 > The Adapted Code (Copy-Paste)

1️⃣ Action : Use this modified block specifically for your site structure.

2️⃣ Utility : Hardcoding the Organization name and Logo reinforces brand identity in Google's Knowledge Graph.

3️⃣ Explanation : I replaced generic variables with fixed values for the organization. Make sure to replace the Logo URL with yours.



✔️ Execution Procedure:

Explanation
: Replace [10, 12, 15] with your real IDs and update the Logo URL.

Code:
<xf:if is="in_array($thread.node_id, [10, 12, 15])">
    <xf:head option="ld_json_techarticle">
        <script type="application/ld+json">
        {
          "@context": "https://schema.org",
          "@type": "TechArticle",
          "headline": "{$thread.title|escape('json')}",
          "image": "https://www.your-site.com/images/logo_og_image.png", 
          "author": {
            "@type": "Person",
            "name": "{$thread.User.username|escape('json')}",
            "url": "{$xf.options.boardUrl}/members/{$thread.User.user_id}/"
          },
          "publisher": {
            "@type": "Organization",
            "name": "Your Board Name",
            "logo": {
               "@type": "ImageObject",
               "url": "https://www.your-site.com/styles/default/your-style/logo.png"
            }
          },
          "datePublished": "{{ date($thread.post_date, 'c') }}",
          "dateModified": "{{ date($thread.last_post_date, 'c') }}",
          "description": "{$thread.FirstPost.message|snippet(160)|strip_tags|escape('json')}"
        }
        </script>
    </xf:head>
</xf:if>



⚠️ Troubleshooting (Failsafe):

If error
: Google shows a "Missing field: image" error.

Fix : The main "image" field (line 7) must be representative of the tutorial. If you don't have a "Cover Image" addon, leave a default site image, otherwise Google will ignore the Rich Snippet.

Code:
"image": "{$thread.CoverImage ?: 'https://www.your-site.com/images/default_tuto.jpg'}",



🆚 Differences Between Versions

In generic scripts, you might see {$xf.options.boardTitle}. That's risky if you ever change your board title to something like "Maintenance - My Forum". Here, we carved the brand name into the code marble. It's more pro.



🎁 Bonus Tip

I added |strip_tags in the description. If your first post starts with a Bold tag or an image, this prevents polluting the Google description with raw HTML code.



🚫 What NOT to do

Don't forget to check the Image URL. If you put a relative URL (e.g., /images/logo.png) without the https://www.your-site.com in front, the Schema.org validator will reject you. The URL must be absolute.



🧠 Pro Tip

Once in place, pass a tutorial URL through Google's Rich Results Test tool. If you see "TechArticle detected" in green, you win.



📚 For Further Reading

Google Rich Results Test

XenForo 2 Conditional Statements Guide

Schema.org TechArticle Definition

Search with Google



📝 Bottom Line

I adapted the script to be multi-zone (via the ID array) and bulletproof regarding URLs. Copy this into thread_view, just change the image URLs and IDs, and your SEO will take steroids. Now, go deploy.
 
I asked Andy to take a look at that and see if an add‑on can be created or if it needs to be implemented directly in the XenForo source code.
 
Back
Top Bottom