About page with list of blog posts

Hello community!

I’m a Junior Dev and I’m tasked with adding a list of blog posts published by an author for each profile page. I was reading that the blog_author listing function won’t work unless “…you are looking at an author listing page”. What I’ve tried to do is:

  1. create a custom module
  2. iterate through the content
  3. if content.blog_post_author = “authorName”
  4. print out the stuff

So far, I haven’t been able to display anything. Is it even possible? I feel like I’m going down a “rabbit hole” and want to stop before I waste too much time.

There is a workaround. I can add a link to the blog listing page for a given author (i.e. …/blog/author/joe-person), but prefer not to do it unless I have to. Any help would be greatly appreciated.

Thanks all!

Hey @lymanw I believe this function is what you’re looking for. Let me know if you have any questions after giving it a try!

jasonrThanks! It worked like a charm. This is what I ended up with so far:

<h2>Recent Posts</h2>
{% set author_posts = blog_recent_author_posts(‘default’, “firstName-lastName”, 5) %}
{% for author_post in author_posts %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
</div>
{% endfor %}

As you can see the name of the author is hard-coded into the module, which is not ideal. I have another module on the same page that has a variable called {{widget.team_member_name}}.

Follow-up question.

How can I access widget.team_member_name from the other module, @JasonRosa?

Hey, @lymanw for clarification, you’re basically trying to rotate in a value from a different module in for the first name/last name automatically? Is that correct? That should be possible but how to add that value in would depend on the context. Where are you currently setting the widget.team_member_name value and where are you setting the author_posts function? Is it in the same module or in two different places?

Overall I think something like this should work:

<h2>Recent Posts</h2>
{% set author_posts = blog_recent_author_posts(‘default’,widget.team_member_name, 5) %}
{% for author_post in author_posts %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
</div>
{% endfor %}

But the exact code for the widget.team_member_name might change depending on where it is being set. If you want to send over your full code/answer the questions above, we should be able to get you closer!

That’s correct, @JasonRosa. The two modules will be on the same page (template). The user will be filling out the following module:

<div class=“padding20tb clearfix”>
<div class=“row”>
<div class=“span3 team-member-headshot text-center”>
{% if widget.team_member_head_shot.src %}
<img src=“{{ widget.team_member_head_shot.src }}” width=“{{ widget.team_member_head_shot.width }}” height=“{{ widget.team_member_head_shot.height }}” alt=“{{ widget.team_member_head_shot.alt }}”>
{% endif %}
<p class=“team-member-social text-center”>
{% if widget.facebook_link %}
<a href=“{{ widget.facebook_link }}”><i class=“fab fa-facebook-f”></i></a>
{% endif %}
{% if widget.linkedin_link %}
<a href=“{{ widget.linkedin_link }}”><i class=“fab fa-linkedin-in”></i></a>
{% endif %}
{% if widget.twitter_link %}
<a href=“{{ widget.twitter_link }}”><i class=“fab fa-twitter”></i></a>
{% endif %}
</p>
</div>
<div class=“span9”>
<h3 class=“team-member-name”>{{ widget.team_member_name }}</h3>
<h5 class=“team-member-title”>{{ widget.team_member_title }}</h5>
<p class=“team-member-bio”>{{ widget.team_member_bio }}</p>
</div>
</div>
</div>

I would like the value of {{ widget.team_member_name }} from this module to populate in another module:

<h2>Recent Posts</h2>
{% set author_posts = blog_recent_author_posts(‘default’,{{ widget.team_member_name }} , 5) %}
{% for author_post in author_posts %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
</div>
{% endfor %}

So that it the second module will print blog posts published by the author.

Hey @lymanw thanks for the clarification. Putting these two items in different sections makes things a little bit tricky as you can’t pass a value from one custom module into another custom module to my knowledge. Is there any way that you would be able to have these two sections in the same custom module or would they have to be separate?

I am willing to try that solution. What would the it look like?

Hey @lymanw sorry for the delay! I believe that it would look almost exactly like your current code. The main difference with this being in the same module is that you can pull in the values from the module into your HubL function. Let me know if you have any issues when giving that a try!

<div class=“padding20tb clearfix”>
<div class=“row”>
<div class=“span3 team-member-headshot text-center”>
{% if widget.team_member_head_shot.src %}
<img src=“{{ widget.team_member_head_shot.src }}” width=“{{ widget.team_member_head_shot.width }}” height=“{{ widget.team_member_head_shot.height }}” alt=“{{ widget.team_member_head_shot.alt }}”>
{% endif %}
<p class=“team-member-social text-center”>
{% if widget.facebook_link %}
<a href=“{{ widget.facebook_link }}”><i class=“fab fa-facebook-f”></i></a>
{% endif %}
{% if widget.linkedin_link %}
<a href=“{{ widget.linkedin_link }}”><i class=“fab fa-linkedin-in”></i></a>
{% endif %}
{% if widget.twitter_link %}
<a href=“{{ widget.twitter_link }}”><i class=“fab fa-twitter”></i></a>
{% endif %}
</p>
</div>
<div class=“span9”>
<h3 class=“team-member-name”>{{ widget.team_member_name }}</h3>
<h5 class=“team-member-title”>{{ widget.team_member_title }}</h5>
<p class=“team-member-bio”>{{ widget.team_member_bio }}</p>
</div>
</div>
</div>

<h2>Recent Posts</h2>
{% set author_posts = blog_recent_author_posts(‘default’, widget.team_member_name , 5) %}
{% for author_post in author_posts %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
</div>
{% endfor %}

Thanks for your reply. I gave it a go and I was not able to get the articles to display. This is code for the module. Note that I had to add a bit of formatting for the name because widget.team_member_name saved the value as a string with a space between the first and last name. The text for {{ hyphenatedName }} does display in “Recent Posts from {{ hyphenatedName }}”, but it doesn’t use the same variable to pull the list. If I hard-coded the text, it works. Thoughts?

<div class=“padding20tb clearfix”>
<div class=“row”>
<div class=“span3 team-member-headshot text-center”>
{% if widget.team_member_head_shot.src %}
<img src=“{{ widget.team_member_head_shot.src }}” width=“{{ widget.team_member_head_shot.width }}” height=“{{ widget.team_member_head_shot.height }}” alt=“{{ widget.team_member_head_shot.alt }}”>
{% endif %}
<p class=“team-member-social text-center”>
{% if widget.facebook_link %}
<a href=“{{ widget.facebook_link }}”><i class=“fab fa-facebook-f”></i></a>
{% endif %}
{% if widget.linkedin_link %}
<a href=“{{ widget.linkedin_link }}”><i class=“fab fa-linkedin-in”></i></a>
{% endif %}
{% if widget.twitter_link %}
<a href=“{{ widget.twitter_link }}”><i class=“fab fa-twitter”></i></a>
{% endif %}
</p>
</div>
<div class=“span9”>
<h3 class=“team-member-name”>{{ widget.team_member_name }}</h3>
{% set hyphenatedName = widget.team_member_name.replace(’ ', ‘-’) %}
<h5 class=“team-member-title”>{{ widget.team_member_title }}</h5>
<p class=“team-member-bio”>{{ widget.team_member_bio }}</p>
</div>
</div>
</div>

<h2>Recent posts from {{ hyphenatedName }}</h2>
{% set author_posts = blog_recent_author_posts(‘default’, hyphenatedName , 5) %}
{% for author_post in author_posts %}
{% if author_post %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
{{builtin_body_classes}}
</div>
{% endif %}
{% endfor %}

Hey, @lymanw would you be able to link me to the custom module that you’re building or take a screenshot with the right-hand sidebar included? It might be easier for me to see what might be happening that way.

Here’s the link to the module. Thanks again for your help. @JasonRosa

Thanks! Can you give this a try:

<div class=“padding20tb clearfix”>
<div class=“row”>
<div class=“span3 team-member-headshot text-center”>
{% if widget.team_member_head_shot.src %}
<img src=“{{ widget.team_member_head_shot.src }}” width=“{{ widget.team_member_head_shot.width }}” height=“{{ widget.team_member_head_shot.height }}” alt=“{{ widget.team_member_head_shot.alt }}”>
{% endif %}
<p class=“team-member-social text-center”>
{% if widget.facebook_link %}
<a href=“{{ widget.facebook_link }}”><i class=“fab fa-facebook-f”></i></a>
{% endif %}
{% if widget.linkedin_link %}
<a href=“{{ widget.linkedin_link }}”><i class=“fab fa-linkedin-in”></i></a>
{% endif %}
{% if widget.twitter_link %}
<a href=“{{ widget.twitter_link }}”><i class=“fab fa-twitter”></i></a>
{% endif %}
</p>
</div>
<div class=“span9”>
<h3 class=“team-member-name”>{{ widget.team_member_name }}</h3>
{% set hyphenatedName = widget.team_member_name.replace(’ ', ‘-’) %}
<h5 class=“team-member-title”>{{ widget.team_member_title }}</h5>
<p class=“team-member-bio”>{{ widget.team_member_bio }}</p>
</div>
</div>
</div>

{% set authorname = module.team_member_name|replace(’ ', ‘-’)|lower %}

<h2>Recent posts from {{ module.team_member_name }}</h2>
{% set author_posts = blog_recent_author_posts(‘5795150278’, authorname, 5) %}
{% for author_post in author_posts %}
{% if author_post %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
{{builtin_body_classes}}
</div>
{% endif %}
{% endfor %}

Basically, in this version we’re using the text field you already have in the module for the author name. We’re then using some filters to turn the name into the slug needed to make the function run. Please let me know if you have any questions from here!

@lymanw sorry for the delay! I thought I sent this over a couple of weeks ago but my message may have never gone through. Try this for your code:

<div class=“padding20tb clearfix”>
<div class=“row”>
<div class=“span3 team-member-headshot text-center”>
{% if widget.team_member_head_shot.src %}
<img src=“{{ widget.team_member_head_shot.src }}” width=“{{ widget.team_member_head_shot.width }}” height=“{{ widget.team_member_head_shot.height }}” alt=“{{ widget.team_member_head_shot.alt }}”>
{% endif %}
<p class=“team-member-social text-center”>
{% if widget.facebook_link %}
<a href=“{{ widget.facebook_link }}”><i class=“fab fa-facebook-f”></i></a>
{% endif %}
{% if widget.linkedin_link %}
<a href=“{{ widget.linkedin_link }}”><i class=“fab fa-linkedin-in”></i></a>
{% endif %}
{% if widget.twitter_link %}
<a href=“{{ widget.twitter_link }}”><i class=“fab fa-twitter”></i></a>
{% endif %}
</p>
</div>
<div class=“span9”>
<h3 class=“team-member-name”>{{ widget.team_member_name }}</h3>
{% set hyphenatedName = widget.team_member_name.replace(’ ', ‘-’) %}
<h5 class=“team-member-title”>{{ widget.team_member_title }}</h5>
<p class=“team-member-bio”>{{ widget.team_member_bio }}</p>
</div>
</div>
</div>

<h2>Recent posts from {{ widget.team_member_name }}</h2>
{% set authorName = widget.team_member_name|replace(’ ', ‘-’)|lower %}
{% set author_posts = blog_recent_author_posts(‘default’, authorName , 5) %}
{% for author_post in author_posts %}
{% if author_post %}
<div class=“post-title”>
<a href=" {{author_post.absolute_url}}" > {{author_post.name}}</a>
{{builtin_body_classes}}
</div>
{% endif %}
{% endfor %}

It worked. Thanks!

hi
i was using your code for author recent post two make a slider with dynamic name but the details of author like author name and author image is not showing me in page can you help me for this.
i’m posting the code which i’m using
<div class=“authorSlider mainSlider”>
<div class=“wrapper”>
<div class=“authorSliderMain”>
{% if module.author_section_title %}
<div class=“authorSliderTitle”><h2>{{ module.author_section_title }}</h2></div>
{% endif %}
<div class=“authorSliderInner”>
{% set authorName = module.team_member_name|replace(’ ‘, ‘-’)|lower %}
{% set author_posts = blog_recent_author_posts(module.blog_field, authorName , 5) %}
{% for author_post in author_posts %}
{% if author_post %}
<div class=“w33”>
<div class=“ib vt p10-0 p0-15 mw100”>
<div class=“relatedPostBox”>
<div class=“ImageBoxOverlay bg” style="background-image:url(’{{author_post.featured_image }}‘)“>
<div class=“InnerOverlayContent text-center”>
<a href=”{{author_post.absolute_url}}" class=“overlayLink”></a>
</div>
</div>
<div class=“relatedPostBoxContent equal_height”>
<div class=“blog-Title”>
<h5 class=" bodyBackground font-600 “><a href=”{{author_post.absolute_url}}“>{{ author_post.name }}</a></h5>
<a href=”{{author_post.absolute_url}}" class=“overlayLink”></a>
{{ author_post.post_list_content|safe|truncatehtml(90,’…‘,true) }}
</div>
<div class="blogPostAuthorDetails ">
{{author_post.blog_post_author}}
<div class=“blogPostAvatar”>
<div class=“hs-author-avatar” style="background-image: url(’{{author_post.blog_author.avatar}}‘)“></div>
</div>
<div class=“blogPostRightDetails”>
<a class=“author-link” href=”{{ group.absolute_url }}/author/{{ author_post.blog_post_author.slug }}">{{ author_post.blog_author.display_name }}</a>
<div>
<span class=“pulishDate”>{{author_post.publish_date|datetimeformat(’%e %B %Y’) }}</span> | <span class=“minsRead”>4 mins to read</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>