Jump to content

Going in circles with a function


Joss
 Share

Recommended Posts

I Have a working function (Yippeee!) 

Well, it should work, I just stole it from sitemap.

Basiclally, I am using it to display all the children from any point in my page tree, downwards.

The function (which is in an included page) goes:

function getChildCategories($page) {
    echo "<ul>";
    echo "<li><a href='{$page->url}'>{$page->title}</a> ";    

    if($page->numChildren) {        
        foreach($page->children as $child) getChildCategories($child);         
    }
    echo "</li>";
    echo "</ul>";
}

and I call it like:

getChildCategories($page); 
 

I want to do a couple of things to change it, and that is where I get stuck.

One

For my other functions, I am using wire("page") rather than $page as suggested by Soma.

But I cant get that to work here. For consistency, how can I change it so I don't need to pass the $page variable?

Two

I want to wrap it in a div as part of the function. This is also to be consistent with what I am doing elsewhere. I am assuming I need to get an output somehow and then return it with divs wrapped round the output. I tried changing echos to $out .= , but I think I got stuck in a never ending loop!

Anyway, my lack of knowledge has defeated me.

Three

(oh, I meant three)

I want to do something similar for calling posts.

So, I have three pages in my tree which are being used as categories - each the child of the other.

My posts use a page field for the category and I grab the posts simply by comparing the page field with the current $page - normal stuff.

I want to now find all the posts that match the children of the current page, all the way down the tree till I fall off the end. And then, return the 10 most recent ones, or something like that.

But initially, just get hold of them. 

I am assuming something recursive has to happen to return an array that I can then use find() with?

Again, lack of knowledge .....

Thanks to any of the usual Gurus!

Joss

Link to comment
Share on other sites

Not really sure if it's exactly what you looking for but these should get you there.

// recursively get all children from current page

function getChildCategories($page=null, $level=0) {
    if(!$page) $page = wire('page');
    $level++;
    $out = '';
    $out .= "\n\t<li><a href='{$page->url}'>{$page->title}</a>";
    if($page->children->count()){
        $out .= "\n\t<ul>";
        foreach($page->children as $child){
            $out .= str_replace("\n\t", "\n\t\t", getChildCategories($child, $level));
        }
        $out .= "\n\t</ul>\n\t";
    }
    $out .= "</li>";
    if($level == 1) return "<div><ul>$out\n</ul>\n</div>";
        else return $out;
}

echo getChildCategories();
 
 

And from your point 3. "has_parent" is the key here I think to find all children recursively from a certain page, then get all posts that has at  least one of those selected

// get all children categories of a parent and get most recent posts from all those

function getChildCategoriesPosts($page=null) {
    if(!$page) $page = wire('page');
    $out = '';
    $categories = wire("pages")->find("has_parent=$page,template=category");
    $posts = wire("pages")->find("category=$categories, sort=-modified, limit=10");
    if($posts->count()){
        foreach($page->children as $child) $out .= "<li><a href='{$child->url}'>{$child->title}</a></li>";
    } else {
        return "<li>No Posts found/p>";
    }
   return "<div><ul>$out</ul></div>";
}

echo getChildCategoriesPosts();
 
  • Like 1
Link to comment
Share on other sites

HI Soma

First works perfectly - and I have learned a lot from that, thanks

Second one was more problematic.

It didn't do anything initially, but I have changed is slightly because in your foreach I think you mean $posts not $page->children?

Changing it worked but I ran into a problem when I tried it on a page that is at the end of branch - it kind of listed everything including most of the contents of the British library!

So I added an IF which I think is right?

Anyway, here is the final

function getChildCategoriesPosts($page=null) {
    if(!$page) $page = wire('page');
    $out = '';
    $categories = wire("pages")->find("has_parent=$page,template=category");
    if($categories->count()){
        $posts = wire("pages")->find("post_category_select=$categories, sort=-modified, limit=10");
        if($posts->count()){
            foreach($posts as $post) $out .= "<li><a href='{$post->url}'>{$post->title}</a></li>";
        } else {
            return "<li>No Posts found/p>";
        }
    }
   return "<div><ul>$out</ul></div>";
}

Thanks again!

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