Hi, is there a way to remove the link from a blog authors name if they have only written one blog? Currently all blog authors names link to a page deicated to all the posts they have written.
Is there a way to rewrite the above code so that it checks to see how many blogs the author has written? Or is there a way in the Hubspot blog author table to added a checkbox to turn this feature on or off depending on the number of blogs an author has written?
Thanks
Hey @Woodsy! Thanks for your post. Let’s invite some of our experts to see if they have any suggestions!
@Jnix284, @Anton, @DaniellePeters -- do any of you know how we might remove the link from the author name on select blog posts? Ideally, we could make this dynamic based on the total blog posts they’ve written. All workarounds are welcome! Thanks
Hello @Woodsy!
That’s an interesting request! I haven’t done this myself but it should be possible if you use blog_recent_author_posts and check the length of the returned value.
Try something like this:
{% set author_posts = blog_recent_author_posts("default", content.blog_post_author.slug, 5) %}
{% if author_posts|length > 1 %}
<h4>Written by <a class="author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}">{{ content.blog_post_author.display_name }}</a></h4>
{% else %}
<h4>Written by {{ content.blog_post_author.display_name }}</h4>
{% endif %}
The code above stores the 5 most recent articles for the author of the blog under the author_posts variable and then checks its length to decide if it should show a link or not.
If the author has more than 1 article, it will show the link, otherwise it won’t.
If I remove this part of the code the error resolves:
{% set author_posts = blog_recent_author_posts("default", content.blog_post_author.slug, 200) %}
Not sure what to do as the error isn’t giving me much to go on. Ihave tried placing this line of code above the other code. I have also placed it at the start of the HTML.
Thank you in advance for any further guidance.
you can try using the blog_recent_author_posts function for this.
The code would look something like this:
{% set author_posts = blog_recent_author_posts("default", content.blog_post_author.slug, 200) %}
{% if author_posts > 1 %}
<h4>Written by <a class="author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}">{{ content.blog_post_author.display_name }}</a></h4>
{% else %}
<h4>Written by {{ content.blog_post_author.display_name }} </h4>
{% endif %}
I’d recommend to put this code inside a custom module since it will give you more flexibility and (a bit better usability) in the long run. It would also allow you to add a lot of modifcations for later use.
For instance:
You could add a blog select option if you have several blogs (or planning to have several ones)
You could add a number field to manually change the amount per blog listing. For instace you’d like to check for more than 1 post in “BLOG A” but for more than 5 posts in “BLOG B”…