Jump to content

how to implement the url like wordpress ?


adrianmak
 Share

Recommended Posts

You would use url segments on your home page. Then from the segments, build a query using $pages->get("selectors") to return a page from this. You'd probably want to use /2015/01/05/ to select the page by date using the "created" field, and page-title-name to select the page by the "name" field, which is used for urls.

It would probably look something like that (note that I didn't test this):

$year = $urlSegment1;
$month = $urlSegment2;
$day = $urlSegment3;
$name = $urlSegment4;

# Assuming your blog posts use a template called "post"...
# You need to do >= and < on created because it's using timestamps, 
# so you're looking for something in a range
$page = $pages->get("template=post, created>={$year}-{$month}-{$day}, created<{$year}-{$month}-{$day+1}, name={$name}");

if(!page) {
    // redirect to 404 or another page or do something else
}

. . .

I personally wouldn't use a directory structure for this, it just makes moving things harder. In WP, these folders don't really exist really either.

Also note that you could probably just skip checking the creation date, since page names (url, not to be confused with the title field) are already unique by default. But you might want it anyway for some reason.

  • Like 2
Link to comment
Share on other sites

The first (maybe naive) structure that come my ming is as seen on the screenshot below

tPfoDe2.png

You may also want to look at the blog module here.

I'm wondering this method is not quit user friendly, user have to create month, day page before create a blog post.

I think Pierre-Luc way is the right direction instead.

Link to comment
Share on other sites

I just read thru the url segments documentation. It given example of url segments are appended on actual url.

My case is opposite. url segments is prepended of a actual url.

Not a problem in this case. You would use the template of the parent page, "posts" in this example:

Posts

    post1

    post2

    post3

The post pages themselves wouldn't need a template, since they wouldn't be shown directly.

The only problem I see with this approach is that you wouldn't be able to use the $page->url method for getting this pages, but create your own method to generate the new urls

Anyway, I'm not sure that these WP urls are really better than mysite.com/posts/post-name

  • Like 3
Link to comment
Share on other sites

As I explained earlier, you will need to use a range (min and max values) on created because it uses timestamps to match dates. That would look like created>=2014-01-01, created<2015-01-01. PW doesn't understand only years, you'll need to use properly formatted dates.

Link to comment
Share on other sites

Having to create month and days beforehand sure is not as user friendly as one can imagine i agree with that.

You can explorer Pierre-Luc's or Diogo's solution as with Processwire there's more than one way to do it.

You can also get great insights on the process of migrating a site from Wordpress to Processwire exposed by Ryan himself.

Link to comment
Share on other sites

I think a balanced solution would be staying in the middle of both solutions discussed. Create pages for the years and use the url segments on those pages instead of the parent "posts" page:

posts

    2015 <-url segments would be used on the template for these pages

        post5

        post4

        post3

    2014

        post2

        post1

This would make it easier to use the "posts"  for listing the posts and use the year pages only for the purpose of displaying the individual post pages. Also, you could reduce the url segments to 3 (the default) instead of 4.

You don't really need to use a range of timestamps to decode the url. All you need for getting the right post is the last segment (the name), and you can use the day and month to validate the page, by confirming that they match the post's date and throwing a 404 when it doesn't. The reverse (creating the urls) is just as easy. You can, for instance, display the parent's url (mysite.com/2015/), add the month and day using the posts date and PHP's time function and add the post's name in the end.

I'm not giving code examples because you said you would like to write your own code for learning purposes ;)

Link to comment
Share on other sites

For dates I would suggest adding a date/time field to your template. Call it "post_date" for example. Have it default to today's date. That way you don't have to set it, but you can adjust it if needed. This lets you pre/post date your entries easily.

Now instead of using "created" in your selector, use the name of your new date/time field "post_date".

  • Like 1
Link to comment
Share on other sites

For dates I would suggest adding a date/time field to your template. Call it "post_date" for example. Have it default to today's date. That way you don't have to set it, but you can adjust it if needed. This lets you pre/post date your entries easily.

Now instead of using "created" in your selector, use the name of your new date/time field "post_date".

As you suggested, I added a post date on blog post template.

However, the default value only show the date, how to default both date with time ?

post-2272-0-81089600-1422197441_thumb.pn

Link to comment
Share on other sites

@adrianmak, for all Fieldtypes (page fields, date/time field, colour picker, etc, etc...both custom and core) if you want to check if they have settings that can be configured by the user, edit that field and check out their 'Details' and 'Input' tabs. Please try that and let us know if you still can't get time to display in your 'Post Date'. 

Edit: Beaten by Diogo :-)

Edited by kongondo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...