Jump to content

Blog and categories


adrian_gp
 Share

Recommended Posts

Hello everyone,

I'm new in this forum and my english is not perfect so sorry for the mistakes.

I have an Blog Project and i will use categories. 

Here is the structure:

  • Blog -> displays all the Blogposts (template=BlogPage)
    • Post-Name -> display the specific Post (template=BlogPost)
  • Categories -> not visible in the menu (template=CategoriesPage)
    • Categorie-Name -> not visible in the menu (template=CategoriePage)

For your information:

  • (BlogPost) every blogpost has its own page
  • (BlogPost) in Admin i can select the categorie per Field -> SelectMultiple Pages (Checkbox) (fieldname=categories)
  • (Blog) displays all the BlogPosts and all the categories under the ParentTemplate CategoriesPage, which also have their own side

Now the Problem:
On Blog -> I would like to click on a category and only see the post that has this category checked in the BlogPost.

CategoriePage -> what do I have to insert there to show only the specific posts which use the checked categorie?


Let me know if you need more information

Thanks for your support

 

Link to comment
Share on other sites

Hi @adrian_gp,

Welcome to the forums.

Let's say you have a category 'Technology' whose URL is /categories/technology/.  In the template file of the template CategoriePage, you can do this:

// @note: we set a limit here. You can use pagination if you have many posts
$posts = $pages->find("template=BlogPost, limit=10, categories=$page");

$out = '';
// category title
$out .= "<h2>{$page->title}</h2>";
// found posts in this category
if($posts->count) {
    foreach($posts as $post) {
        $out .= "<h3><a href='{$post->url}'>{$post->title}</a></h3>" .
         "<p>{$post->summary}</p>";// assuming you have a summary field
}
// no posts in this category yet
else $out .= "<p>No posts found in this category</p>";

echo $out;

Quickly typed in the browser, check for errors, just an example.

 

Btw, there are blog profiles and a Blog module you can look at for more examples.

  • Like 4
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...