Table of category pages

Post Reply
saum
Jr. Bludit
Posts: 7
Joined: Thu Oct 17, 2019 8:15 am

Hi,

I'm currently using the theme Alternative and would like to have a static page with link to it on top right (like the About page). On this page I want to present the links to all pages in a specific category.
Currently I am editing this page and creating the links for the category pages manually.

Is it possible to have something like a table of contents on a page for categories, which is created automatically?
Something like this:

Title
desc

Title
desc

Thanks in advance!
User avatar
Jay
Master Bludit
Posts: 133
Joined: Mon Feb 11, 2019 8:41 pm

Shouldn't be hard to achieve. Try this

1. create a new, empty static page to list the cat content with an example name catpages
2. check below example code, adjust it to your own requirements, and place somewhere in page.php file located in theme directory

Code: Select all

<?php
if ($page->slug() == 'catpages' && $page->isStatic() ) {
	$category = getCategory('YourCategory');
	foreach ($category->pages() as $pageKey) {
		$page = new Page($pageKey);
		echo '<h3>'.$page->title().'</h3>';
		echo '<p>'.$page->description().'</p>'; 
	}
}
?>
saum
Jr. Bludit
Posts: 7
Joined: Thu Oct 17, 2019 8:15 am

Thanks Jay, this worked nicely.

The link to the respective page I got with

Code: Select all

$page->permalink(false)
One further question:
Is it also possible to create some kind of snippet for this, which could be inclueded in the WISIWYG editor and would then be replaced with this table of contents?

Thanks and happy new year!
User avatar
Jay
Master Bludit
Posts: 133
Joined: Mon Feb 11, 2019 8:41 pm

Just posted a proof of concept to what you wanted to achieve, but you already used respective function to get the url.

When it goes to parsing php code within wysiwyg editor I wouldn't hit it for security reasons. But afaik there are some plugins doing such thing but you would have to check them by yourself and modify accordingly.

From the other hand this could be achieved with bludit's basic functionality if you only want to get such "TOC" on top or at the end of article.
Just look for a thread about "template" field in article options. You can use it to include appropriate template file (let's say toc.php) before article's content along with article's customfield for a category name.
Post Reply