Jump to content

Blog categories with page field


DarkwaveSurfer
 Share

Recommended Posts

Hi,

I'm total newbie for php and Processwire so be patient with me:lol:

I'm trying to make a simple blog. 

BLOG - list of all posts   (template: blog-list)

-- Post no.1   (template: blog-entry)

-- Post no.2   (template: blog-entry)

-- Post no.3   (template: blog-entry)

CATEGORIES   (template: categories)

--  Jobs   (template: category)

-- Technology   (template: category)


Template for post "blog-entry" has Page field set to Multiple pages and input as Checkboxes. So it is posible to select CATEGORIES children pages via checkbox when editing blog post.

This is how I show selected categories within blog-entry template for current blog post:

foreach ($page->categories as $category) {
	echo "<a href='{$category->url}'>{$category->title}</a>";
}

How do I show selected categories within blog-list template?
I need list of all posts with their Title, body and selected categories from Page field checkbox. I managed to show title and body but not categories.
This is my code so far:

<?php
  echo "<h1>" . $page->get('headline|title') . "</h1>";
  $entries = $pages->find('template=blog-entry')->sort('-created');
  foreach($entries as $entry) {
	echo "<a href='{$entry->url}'>";
	echo $entry->categories->title; //not working
	echo "<h2>{$entry->title}</h2>";
   	echo $entry->body;
   	echo "</a>";
   };
?>

Also how do I show on Jobs page (template: category) list of all posts that have jobs category selected?

Thanks.

Slika zaslona 2016-10-21 u 19.25.27.png

Link to comment
Share on other sites

Hi,

"echo $entry->categories->title; //not working"

Does not work, since "categories" is not a Page object, but a collection of Page objects, a PageArray:

http://processwire.com/api/arrays/page/

What you need is the code snippet you used on the page but this time the collection can be accessed via $entry, because you named it as such:

foreach ($entry->categories as $category) {
    echo "<a href='{$category->url}'>{$category->title}</a>";
}

 

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