Jump to content

Url Segments


Joss
 Share

Recommended Posts

Well, actually it in in my header since it really needs to work through all the site.

The trick is to make it work so it follows the false logic imposed by our URL segments.

Actually, it gets worse than that.

If you search for a post, then click on its link, you are back to square one.

So, I suppose the ultimate bread crumb solutions will go something like this:

  • Check if there is a category/page field (or whatever is the relevence for that particular site structure). In my case it would be looking for a value in post_category_select. 
  • If the field exists and there is a value, then that value is what we want to be the parent - work your way up the list and print out the breadcrumb trail
  • If no value, then this is not a post, it must be something in the main site tree, so use normal breadcrumb system.

That takes away the emphasis from the referring page, and introduces rules that are relevant to the actual viewed page.

Maybe adding URL segments to that would not be helpful - or maybe a way. Not sure.

Link to comment
Share on other sites

I'm a bit confused. This is the scenario that I'm seeing here.

  • When you are on the category page, you have this url: domain.com/news/politics/ and this breadcrumb: home>news
  • When you are on the post page you have this url: domain.com/news/politics/political-post and you want to have this breadcrumb: home>news>politics

When using url segments you are still on the same page (fake parent), and the $page variable holds the value of that page. And this is what you want to have on the end of the second breadcrumb. No?

Link to comment
Share on other sites

Ah, that is not what I am getting.

On the politics category page I am getting:

domain.com/news/politics and the breadcrumb home > news (I could add the "politics" with an extra <li> but I haven't added that to your code.)

When I click on a post, the url is:

domain.com/news/politics/politic-post

but the breadcrumb is:

Home > Content Management > Posts > Political Post

My URL segment code at the top of the category page is:

if($input->urlSegment1){
    //get the post
    $post = $pages->get("/content-management/posts/$input->urlSegment1/");
    if(!$post->id) throw new Wire404Exception();
    echo $post->render();
}else{

Category....
 
Link to comment
Share on other sites

  • When you are on the category page, you have this url: domain.com/news/politics/ and this breadcrumb: home>news

Ah, that is not what I am getting.

On the politics category page I am getting:

domain.com/news/politics and the breadcrumb home > news

looks the same to me  :lol:

Link to comment
Share on other sites

I don't get it. If you put the code for the breadcrumb exactly as it is on the default pw site on the header you always have home>news, independently of the presence of the url segment, right?

Link to comment
Share on other sites

I think the problem is about WHERE in the code the breadcrumb code is happening.

Because (obviously) it is within the template, and with the post we are rendering the entire template, then it is picking up the page reference from the post template, not the category template,

Link to comment
Share on other sites

The trouble is that posts are not always viewed in relation to a category, so they need the header and footer.

My only other option is to not render the result, but reproduce the post template within the category template without head and foot. Though that leaves two versions to maintain.

The other way, I suppose, is to do the breadcrumb outside of the template markup (alongside the urlsegment markup - though not part of it) and then import the result into the correct position...

Link to comment
Share on other sites

If the structure of the page is the same, I don't see why you shouldn't include the header and footer in the categories template. People will never access the posts directly, so you don't have to keep them on the post template.

Link to comment
Share on other sites

The trouble is they will access them directly - through search, if nowhere else.

But also, in my particular case, through other sort options - categories is only one.

Also, page structure may vary considerably from category to post - different things in sidebars, or sidebars on different sides ... etc.

Might even have different headers and footers, for all I know

So, for the wiki, I need to keep this as broad as possible and allow for posts to be accessed in several ways, not just through one category type system.

Link to comment
Share on other sites

Okay, I have done a workaround, though I am not going to put this on the wiki - far too specific!

Basically, at the head of my category page, within the if($input->urlSegment1) clause, I have added the breadcrumb.

Because of the structure of my site, this just appears rather uselessly in the bottom left of my page - but at least it appears!

Then, in my header, I have added if(!$input->urlSegment1) to my normal breadcrumbs so they dont appear if I am messing with URL segments.

Then I have used JQuery to move my sad little orphaned bread crumb and stick it in the right place.

Tatty, but it works!

Joss

PS: One day, when I feel I know a bit more, I will mess around with has_parents to see if I can find a better way of doing this.

Link to comment
Share on other sites

jquery is a solution. I was already thinking of looking for the UL and replace it with a preg_match :)

You can't play with urlSegments here, because the post template that is being called with render() is not aware of the real url.

Link to comment
Share on other sites

Yeah, which is why I was thinking about doing it the other way round - taking the name of the current page, looking at it's "parent" via its category select field, then find the tree parents of that.

I suppose I would have to recurse upwards!

Or just curse, which is what I often end up doing....

Link to comment
Share on other sites

oh RATS!!!!

Stupid me!!!
 

That was easy!!
 

        $parentName = $page->post_category_select;

     foreach($parentName->parents as $parent) {
        echo "<li><a href='{$parent->url}'>{$parent->title}</a> > </li>";
    } 
    echo "<li><a href='{$parentName->url}'>{$parentName->title}</a> > </li>";
    echo "<li>{$page->title}</li>"; 
 
Link to comment
Share on other sites

I have just wrapped it in an IF

<ul class='breadcrumb'>
<?php 

// Check if this page is being accessed using the URL Segment
if($input->urlSegment1) {

// Find the fake parent, which is actually the value of a page field used as a category selector
$parentName = $page->post_category_select;

// Take that value and use the standard bread crumb method to find its parents and print them out.
     foreach($parentName->parents as $parent) {
        echo "<li><a href='{$parent->url}'>{$parent->title}</a> > </li>";
    } 

// Print out the page that was the value of the category page select field
    echo "<li><a href='{$parentName->url}'>{$parentName->title}</a> > </li>";

// Print out the title of the page we are looking at
    echo "<li>{$page->title}</li>"; 

  } else {

// If the page being viewed is not the result of a URL segment routine, use the standard way of generating breadcrumbs.

      foreach($page->parents as $parent) {
        echo "<li><a href='{$parent->url}'>{$parent->title}</a> > </li>";
    } 
    echo "<li>{$page->title}</li>";    
  }
?></ul>
 

This is the solution I thought about many posts ago, but in my head it sounded really complicated, so I didn't go any further.

If I had sat down and worked it out, I would have saved you loads of time.

Sorry Diogo!

EDIT: Commented the code.

Edited by Joss
Link to comment
Share on other sites

Here is a slight alternative way of doing the check.

Above I checked to see if the page was part of the URL segments.

However, in my particular case, I can check to see if I am viewing a particular type of page.

This could be done by checking the template, or in my case, checking the parent, since all my posts go under the same parent.

so, rather than

if($input->urlSegment1) {
 

I could use:

if($page->parent->name == "posts") {
 

The advantage is how ever I link to the page, it always shows the breadcrumb in relation to the category.

However, it should be noted that the URL will be the proper url - so within the site, the same page is linked to potentially twice by two different routes.

Link to comment
Share on other sites

  • 1 month later...

Hey Joss,

Can you explain the page_select from this line that appears in the wiki article you posted. I can't see this selector mentioned anywhere else.

$posts = $pages->find("page_select=$category");
 
Link to comment
Share on other sites

Hi Adrian,

After the code example, I explain that page_select is a page field that has been used to select a category for the post.

So, you create a page field, make is a single select, and then define which pages you are selecting. For instance, you may have a common parent, or you may restrict your selection by template - something so that it is obvious what you are choosing from. In this case, where the categories are pages in the normal page tree, then the parent would be Home and the template would be category (or whatever the template is).

This field would then be added to your post template so you can select the categpry and you can use it as a search parameter in the "find" statement. In my example I am retrieving the id of the current page and then selecting all posts where page_select (my page field) matches the id. 

The API will automatically make the right match whether you use page ID or page name. Its clever like that! :)

Joss

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...