HubL: (Goal) Add Co-Authors in Blog Post


#1

Hi,

My goal is to list co-authors in any blog post.

Since HubSpot doesn't have that inbuilt support, what I am trying to do is:

  1. Choice field with all Authors as options to choose from.
  2. Get Meta of Selected Author like Avatar, Slug, etc.

Once the co-author is selected via the choice field, my intention is to compare it against the authors' list again so that I can get Author Avatar, Author Slug (I hope so). I am not concerned whether the co-author will be credited for the post, at the minimum I need some basic Author meta-data for display.

Example:

For Choice field, I've tried the following code, but it not working.

    {% set ta_authors = blog_authors('default', 250) %}

{% macro keyValues() %}
	[{% for author in ta_authors %}[\"{{ author.id }}\", \"{{ author.display_name }}\"]{% if !loop.last %},{% endif %}{% endfor %}]
{% endmacro %}
{% set keyValues = keyValues() %}
    {{ keyValues }}

{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices='{{ keyValues }}' %}

The Choice list is not being populated. see: https://prnt.sc/kihs0y

After, I tried to use static values in choice

{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices='[[\"3457941083\", \"abbe vogels\"],[\"4756481638\", \"Alhad Akole\"]]' %}

The choice field is displaying choice and I can get the value of the selection.

Now, what I am trying to do to get author avatar and slug is

1. get all authors in sequence var all_auth
2. get the choice field value (auth id), var co_auth
3. loop through the sequence 
    for item in all_auth
         if co_auth == item.id  <-- get the author object
            set co_auth = item  <-- update the variable with the matched author object - possible typecasting issue
         endif
    endfor
4. Get co_auth.avatar & co_auth.slug

I am not having any luck in the iterative loop and if statement so far.

What I am trying to achieve is something like this
https://prnt.sc/kihsii

I just need the selected author from choice to be listed with Author Avatar, Slug so if someone clicks the URL he or she is able to find other live posts of the author.

Thanks in advance,


#2

Any suggestions regarding this?
A pointer to the right direction is also appriciated :wink:


#3

Hi @Praveen_Singh,

Thanks for your patience here; I must have missed this post. I'm not seeing anything obviously wrong with your HubL code; can you send me a link to the blog you're working on so that I can take a closer look?


#4

Hi Derek,

This is preview link for the template (development)

The (production/live) blog can be found at https://www.visiontemenos.com/blog

Thanks,
Praveen


Make a author page with custom fields
#6

Hi @Praveen_Singh,

I dug into your code for a while, but I ended up starting fresh in my own portal. Here's the solution I managed to come up with:

{# Initailize an empty array to store the choices #}
{% set author_choices = [] %}
{# loop through blog's authors #}
{% for author in blog_authors('default', 250) %}
    {# in each loop iteration, create a value/label pair with the author's display name & id #}
		{# Each pair should look like this: ["Display Name","123456"]#}
    {% set author_name_id = '["'+author.id+'","'+author.display_name+'"]' %}
		{# Add the author's name and id to author_choices#}
		{% set author_choices = author_choices + author_name_id %} 
		{# On the last loop iteration, create the choice module with the list of authors #}
    {% if loop.last %}
				{% choice "co_author" label='Select Co-Author', export_to_template_context=True, value='', choices={{author_choices}} %}
    {% endif %}	
{% endfor %}

{# This macro loops through a blog's authors and prints info on the author with the provided ID #}
{% macro print_author_info(author_id)  %}
	{% for author in blog_authors('default', 250) %}
		{% if author.id == author_id %}
			{# Everything inside this if statement is just example markup; here's where you'd put your co-author markup #}
			<div style="background: AliceBlue;">
        <h3>{{ author.display_name }}</h3><img src="{{ author.avatar }}" style="max-width:50px;">
        <p>{{ author.bio }}</p>
			</div>
		{% endif %}
	{% endfor %}
{% endmacro %}

{# Finally, you can call the macro wherever you want your co-author markup to appear in your template #}
{{ print_author_info(widget_data.co_author.value) }}

Let me know if this works for you, or if you have any other questions!


#7

Hi @Derek_Gervais,

Many thanks for your time and effort for looking into this. I was missing the extra loop to search again in blog authors instead I was directly assigning the object, which was not working.

Your code is working perfectly.

Thanks again!

  • Praveen

#8

Hi @Derek_Gervais,

I just want to you to look at below links:

  1. Single Post Page
    https://www.visiontemenos.com/test-blog/will-designs-ever-rule-the-world

  2. Listing Page
    https://www.visiontemenos.com/test-blog

The Template is the same for both listing and blog post.

The macro is not rendering on listing page, even though it is rendering perfectly on the single blog page.

Any scope or template refresh issue?
Thanks,