I would like to add custom fields to a blog post, for example a checkbox called “Featured Post”. Then i would like to have access to this data within hubl code, so i can apply logic in the template
Hi @Demetri,
Do you mean a checkbox for your team/customer who is creating the blog post per the visitors of the blog post who don’t have access to hubspot?
Best,
Anton
The people creating the blog post.
Hi @Demetri,
okay - that’s much easier to do ![]()
You have multiple options:
- create an if-loop inside the blog-post template and wrap it around the featured post code. By doing so the author will get an option(“module”) when creating a new post.
- create a new module with an if-loop and the featured post code. Personally I go almost always this way because it gives you much more flexibility(you could even show a featured post from another blog if you have mutliple ones). After creating this module you place the module where you want to show the optional featured post.
Here some code examples for both solutions
if-loop directly in template
{% boolean "show_featured_post" label='Show Featured post', value=True, export_to_template_context=True %}
...
{% if widget_data.show_featured_post %}
place your featured post code here
{% endif %}
place this code inside your blog-post(not listing) template. The if-loop should be in the place you would like to show the featured post.
if-loop in module and template
module
{% if module.show_featured_post %}
place your code here
{% endif %}
template
...
{% module 'Featured Post' src="PATH-TO-MODULE", no_wrapper=True %}
...
for the “easiest” use I would recommend to right-click the module in the left sidebar of the design-manager and select “Copy snippet”. Place it in the template where you’d like to display the module. Also I recommend to add the no_wrapper=True option at the end of the module. This will minimize the sourcecode slightly by not using unneeded wrapping divs
best,
Anton
How do you bring the custom fields to the listing view? It used to work but it no longer does.
Hope this helps someone in the future as there are still considerable gaps in the documentation regarding this.
In your listing loop, your data in the fields are stored in the module ID… so if your module snippet in the blog post is
{% module "module_55555555555" path="/THEME/modules/CUSTOM MODULE", label="CUSTOM MODULE" %}
then your loop would be something like:
{% for content in contents %}
{{ content.widgets.module_55555555555.value }}
{% endfor %}
If you want to see everything returned from the module you can just output
{% for content in contents %}
{{ content.widgets.module_55555555555 }}
{% endfor %}
In my case, my module had 1 singular “Link” field type field called “external_link”, so to get the URL I had to call:
{% for content in contents %}
{{ content.widgets.module_55555555555.body.external_link.url.href }}
{% endfor %}
and of course, to complicate things, Hubspot will return the content ID if the link is to content and the href if the link is external. But you get the idea.
Another thing to note is that you can see ALL custom fields in your blog listing loop by using
{% for content in contents %}
{{ content.allWidgets }}
{% endfor %}
Again, Hubspot, this should be in the docs.