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:
create a custom module
iterate through the content
if content.blog_post_author = “authorName”
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.
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!
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?
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!
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?
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.
{% 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!
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>