How to collect specific param from config.toml list in shortcode?

Hello everyone.

I have a config.toml that contains the following:

[[params.social]]
  name = "Github"
  icon = "fa fa-github fa-2x"
  weight = 1
  url = "LINK"
[[params.social]]
  name = "LinkedIn"
  icon = "fa fa-linkedin"
  weight = 2
  url = "LINK"
[[params.social]]
  name = "Twitter"
  icon = "fa fa-twitter"
  weight = 3
  url = "LINK"

I want to access the 2nd item’s url in a shortcode in my markdown, but I can’t figure out how.

I know I have to use this shortcode: {{< param author >}} where I replace author with my param refernce, but I can’t figure out how to access my url of specific social link object.

Can anyone help? Thanks!

(index (where site.Params.social "name" "LinkedIn") 0).url

Thanks for your reply! How does your code fit into the {{}} code? Do i replace “author” with what you posted?

If you want to extract this value into your content, you need to write your own shortcode.

I am grateful for your replies, but for future readers’ sake, can you post a full solution?

I was expecting to be able to use the existing param shortcode, can I not?

No, you cannot, because you are accessing a map element in an array of maps.

markdown

{{< social-link "LinkedIn" >}}

layouts/shortcodes/social-link.html

{{ $name := .Get 0 }}
{{ $url := (index (where site.Params.social "name" $name) 0).url }}
<a href="{{ $url }}">{{ $name }}</a>

See https://gohugo.io/templates/shortcode-templates/

4 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.