HubL for Blog URL based on ID

alyssawilie
Participant

In the new design manager - I have a custom recent posts module for use on page templates that also a includes a link to the blog to see more posts. Is there anyway to use HubL to populate that link based on the ID chosen from the Blog selector?

I thought it might work in the same way as {{ page_by_id().url }} but it seems like the Blog selector produces the ID and nothing else.

7 Replies 7
tjoyce
Recognized Expert | Platinum Partner

I think you are forced to loop through posts 200 at a time and do an if statement to see if it matches your blog post id.

Found that in this forum thread

 


tim@belch.io

0 Upvotes
alyssawilie
Participant

Sorry, that's not at all what I'm looking for. I want to get the root blog url based on the main blog ID you can set using one of the new field types in the new design manager's custom module. I don't want anything from the posts.

0 Upvotes
LPM
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Yeah I've noticed this as well.

 

Basically every object type is obtainable using the content_by_id functionality, except for the blog root itself.

 

I was trying to do the same thing when I discovered this issue. Got recent blogs using a combination of the Blog field and blog_recent_posts function, but there's no way to get the blog listing URL from the Blog ID.

 

@alyssawilie if you're really desperate to automate it, and your blog is in a certain set format and urls are always at the same level, you can do something like this:

 

{{ rec_posts[0].absolute_url|regex_replace('/[^/]*$', '') }}

This should remove everything after the last trailing slash to give you the url of your blog.

Jsum
Key Advisor

Hi @alyssawilie ,

 

There is a custom module "selector" field that outputs the selected blog ID:

download.png

You can use it in a recent post function by omitting the curly braces:

{# default token for my created blog field #}
{{ module.blog_field }}

{# token applied to recent posts function #}
{% set rec_posts = blog_recent_posts(module.blog_field, 5) %}

{% for rec_post in rec_posts %}
    <div class="post-title">{{ rec_post.name }}</div>
{% endfor %}

I couldn't find any variables or functions that would give the url of a particular blog. If you only have one blog you can use:

{{ group.absolute_url }}

Otherwise you can get a little hacky and use the blog_all_posts_url fuction with a replace filter to remove "/all" from the url:

{{ blog_all_posts_url(module.blog_field)|replace("/all", "") }}

 

unfortunately, the content_by_id and page_by_id function do not recognize the blog id.

 

let me know if that helps.

LPM
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

@Jsum we've all gotten to that point.

 

The point is that there's no way for us to get the main blog url in the described situation.

 

e.g I have recent blog posts on my home page, how can I get the blog listing url as well?

 

The only solution I've found is the one I've posted above where I have to use regex to remove the last section of a url.

0 Upvotes
Jsum
Key Advisor

@LPM ,

 

My solution is essentially the same as yours, only I didn't need to use regex because I played off of the "all posts" function. It still needs to use a replace filter, but you know what needs to be replaced so it is a simpler replace. Also, using the blog field, you would insert the blog id into the feed function and link function simultaneously anyways so there is no extra work.

 

I agree that there should be a function for generating a listing link from a blog ID, but there isn't. Maybe you should post something in the idea forum.

0 Upvotes
LPM
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Ah sorry, I missed the all post function bit at the end.