Dynamically load blog content into a product page

Hi,

We want to be able to pull in the content from a particular blog on to a product page (the example being in-depth product reviews, written as blog posts, which then we want to pull into the corresponding product detail pages).

Loading the content of one static blog as a proof-of-concept was pretty straightforward:

{% assign article = articles['news/post-handle-here'] %}
{{ article.content }}

However, making that dynamic is proving tricky… My intention was to use a product metafield which contains the handle of the blog I want to pull, but I can’t find a way to drop that into the articles object in a way it can read.

Dropping a metafield within the articles object in any form always results in a liquid error due to unexpected characters.

I’ve tried saving the entire articles string (as in, the full object with the relevant blog handle) in the metafield to get around that, but the article object doesn’t seem to be able to parse it:

{% assign article = product.metafields.custom.product_blog %}
{{ article.content }}

Does anyone have any suggestions or ideas on what would get this working?

Thank you,

Ash

1 Like

Hi @ash-source ,

You cannot use the article metafield like this I believe. You have to loop on the articles if you go this route.

You can do this following instead,

1, Create a product metafield with text type, in this way, you can store the article handle.

  1. Then use the code for reference below (Change the metafield namespace)
{% assign article-handle = product.metafields.custom.product_blog | metafield_text %}
{% assign article = articles[article-handle] %}
{{ article.content }}

2 Likes

Perfect - that solved it exactly. Thank so much.

1 Like