I'm moving over from WordPress, and in order to keep URLs the same I want a date-based URL format for my blog posts, ie example.net/blog/2015/07/01/some-slug
.
However, I can't quite get this to work properly. Anyone got some pointers?
Looks like this will take some editing of the source code, it only supports year/month out of the box.
Well, wasn't very complex. First, set up the URL format on my /blog.htm
page:
title = "Blog post"
url = "/blog/:year/:month/:day/:slug"
is_hidden = "0"
[proBlogPost]
slug = ":slug"
searchpage = "blog"
==
{% component 'proBlogPost' %}
Near the end of plugins/radiantweb/problog/components/Bloglist.php
, change the method so it accepts a :day
parameter as well:
/*
* this must be a date search
*/
if($type == 'canonical'){
$y = $this->param('year');
$m = $this->param('month');
$d = $this->param('day');
$BlogPosts->filterByDate($y,$m, $d);
}
In plugins/radiantweb/problog/models/Post.php
, change the scopeFilterByDate
method to accept our third parameter for the day:
public function scopeFilterByDate($query, $y, $m, $d)
{
return $query
->whereRaw("DATE_FORMAT(published_at,'%Y') = $y")
->whereRaw("DATE_FORMAT(published_at,'%m') = $m")
->whereRaw("DATE_FORMAT(published_at,'%d') = $d");
}
And now we're cookin'.
edit: pretty trivial to make it backwards-compatible with some default values, but I'll save that for later.
You'll have to do some tinkering with the proBlogList
component view so it builds proper URLs for this format.
Last updated
And to render a date-based URL, use this in your proBlogList view:
<a href="/{{ proBlogList.postRender(post.parent) }}/{{ post.published_at|date('Y/m/d') }}/{{ post.slug }}/">{{ post.title }}</a>
And that's it.
Thanks for this, I will look at adding this to master, or at least some very similar variation. I appreciate the suggestion and problem solving. Great job!
1-6 of 6