Jump to content

Output pages matching blog categories


Peter Knight
 Share

Recommended Posts

I have a blog with 3 Categories (dogs,fish,birds) and want to output the most recent posts matching each category.

I played about with some PHP have the first part sort of working in that it's outputting the 3 category titles

<ul>
 <?php
 $latest = $pages->find("template=blog-category");
 foreach($features as $feature) {
 echo  
 "<h3><a href='{$latest->url}'>{$latest->title}</a></h3>" . 
 "<p>{$latest->summary}</p>" 
 ;
}
?>
</ul>

So I feel thats a good start but want to refine it further. Here's my laymans description which I'm not sure how to translate into PW .

find all posts that match the following

template=blog-category

category=dogs

limit= to 2 posts

order=by most recent first

 

I know I could then repeat that twice more where category = cats and category = birds to achieve what I want.

BTW the blog was created with Kongondos blog module. 

 
Link to comment
Share on other sites

Well you seem to have pretty much formulated the selector you want already :) You can get the most recent entries by using the built-in “created” field. And because you want the creation dates in descending order, you prefix that with a hyphen: sort=-created, limit=2.

Like you said, you can repeat this selector 3 times, or maybe you want to foreach over the categories you have already fetched, for a more dynamic approach. In your example: category={$feature}.

$pages->find("template=post, category={$feature}, sort=-created, limit=2";
Or if the posts are children of their categories, you may want to do it even more simply:
$feature->children("template=post, sort=-created, limit=2";
If you have more questions on selector strings, have a look at the docs: http://processwire.com/api/selectors/
Link to comment
Share on other sites

My categories don't seem to have any children in the tree. All pages are children of a parent called Posts.

Beside that, can you explain something regarding the code below

$pages->find("template=post, category={$feature}, sort=-created, limit=2";

Does this selector assume I have a category called feature or is it making a variable called feature out of my categories?

Confused re. selectors as I don't see any documentation on that page re. categories. Only a mention of finding pages with no category :-/

Link to comment
Share on other sites

I assumed $feature to be the variable from the foreach you posted above. I don’t know the blog module you’re using, though. Do you know the PHP syntax at work in that line? The curly braces are optional for readability. As you probably know, a $-sign indicates a variable. In the case of $feature, it holds one of your categories. So for each of the categories you fetched, it finds 2 pages with that category.

Categories are not a “built-in” feature of ProcessWire. The selector I posted assumes your pages have a page-reference field called “category”.

  • Like 1
Link to comment
Share on other sites

 I also don't know the code of the blog module from Kongondo. What I have understand is, that you have some pages belonging to template blog_category. I don't know if dogs, cats, fish, birds are the names of those pages or the titles, so please check and if they are the titles, please correct it in the find selector for the categories

<?php
    // the names (not the titles) of your categories you want to output in the recent list for
    $categories = array('dogs', 'cats', 'birds');
    
    foreach($categories as $category) {
        
        // find the category page and echo the title, [if it not has the template 'blog-category', please change here]
        echo "<h2>" . $pages->get("template=blog-category, name={$category}")->title . "</h2>";

        // find the (limit) latest posts of category=$category, - if there are any, display title as link and a brief summary
        $latestposts = $pages->find("template=post, category={$category}, sort=-created, limit=2";
        // if the above doesn't work, you may try this, if category is a pagefield in the post-page
        //$latestposts = $pages->find("template=post, category.title={$category}, sort=-created, limit=2";
        
        if($latestposts) {
            foreach($latestposts as $latestpost) {
                echo "<h3><a href='{$latestpost->url}'>{$latestpost->title}</a></h3><p>{$latestpost->summary}</p>";
            }
        }
    }
?>

Jan Romero has shown you the right way already, but your code-fragments and your explanation of what you are after are not very clear, at least for people who haven't worked with Kongodos blogmodule. :)

So if it doesn't work, please come back and explain a bit more exact what you have done, which part works and which not. :)

  • Like 2
Link to comment
Share on other sites

Horst - thanks for your time and all the code samples. Actually, i think I've made a great job of over complicating this.

I'm thinking now that my categories are nothing more than a value in a field.

IE I have a field  (drop down box) called categories and the values are cats, dogs and birds. Therefore, I can just use a selector to target and call all posts that have a value matching cats and do the same for dogs and birds.

Going to try it in 5 mins.

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

×
×
  • Create New...