Search code examples
phpoctobercms

OctoberCMS: Why is the slug value missing in "post.url" of the blog plugin


I am using the [blogPosts] component of the blog plugin. I have a for loop to display all blog posts. Everything is fine, but the value of {{ post.url }} is missing the slug value. This is how I have used the component:

url = "/blog/:page?"
layout = "default"

[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 10
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = 404
postPage = "post"
==
{% set posts = blogPosts.posts %}
{% for post in posts %}
<h1>{{ post.title }}</h1>
<p>{{ post.summary|raw }}</p>
<a href="{{ post.url }}">Read more</a>
{% endfor %}

In the above examples, all post links are referring to blog/post. I expect the slug of each post to be in its url, but it's missing. Why?


Solution

  • Sometimes it may be an issue with the blog page URL.

    if you are using [blogPosts] and in markup, you need to generate a link of blog page using {{ post.url }} you need to make proper url of the blog page

    title = "Blog Post Page"
    url = "/blog/post/:slug"
    layout = "default"
    is_hidden = 0
    ==
    <?PHP
      // ... other code
    

    Here important stuff is url = "/blog/post/:slug" and main important thing is :slug parameter

    URL has to have :slug parameter to replace with actual post slug.

    If you also need to add post category slug in URL then you also need to use :category parameter in URL for ex: url = "/blog/post/:category/:slug"

    these :slug and :category is hardcoded in making a link of the post so you need to use exactly them not other names. check below screenshot of post model from rainlab.post plugin.

    post model

    if any doubt please comment.