Vikestart

Multiple parents

6 posts in this topic

I need certain posts to appear on multiple pages.

I use the following code to outpot lists of pages on the category pages:

<?php $stories = $page->children("sort=-date"); if ($stories->count > 0) { ?>
	<div class="cntbox_list">
		<?php foreach($stories as $story) { ?>
			<div class="cntbox_list_item">
				<a href="<?php echo $story->url; ?>" class="cntbox_list_title"><?php echo $story->title; ?></a>
			</div>
		<?php } ?>
	</div>
<?php } ?>

How can I make a post appear in multiple lists like that?

Share this post


Link to post
Share on other sites

Hi @Vikestart

You should use all power of selectors for that.

For example, if you want to output your list of post on home page, you can do

$posts = $pages->get("/categories/)->children("sort=-data");

Read more about selectors in PW

https://processwire.com/api/selectors/

3 people like this

Share this post


Link to post
Share on other sites

Thank you, that's useful info. :) I think I didn't explain the case well enough though.

Basically, in most cases, it's okay for the pages to only have 1 parent and only appear in 1 list on 1 page.

However, some pages need to be listed on multiple parent pages.

Something like this:

 

Home

---- Horror movies

--------- A horror movie

--------- Some horror/thriller movie

---- Thriller movies

--------- A thriller movie

--------- Some horror/thriller movie

 

So let's say Some horror/thriller movie has Horror movies as its parent. How can I make it appear on the Thriller movies page as well, along with that pages other children?

Maybe you already answered this, but do you have a concrete example? :P

Share this post


Link to post
Share on other sites

Hello Vikestart, for that you can use "tags".

To create "tags" there are many options and here is one:

1) create template "tags"
2) create template "tag" with only deafult field "title"
3) create field "tags", type page ( my suggestion is to use Lostkobrakai module Chosen Select ), and add it to "movie" template

*** note: for page field "tags" set as parent page "Tags" (example below)

As example here is some page tree:

- Home
- About Us
- Movies
--- Horror
------ Some Horror Movie (eg. template "movie" with your custom fields - and one is field "tags" (3.) )
--- Thriller
------ Some Thriller Movie ( -//-  -//-  -//- )
- Contact
- Tags ( template: "tags", page/settings: hidden )
--- Horror (template: "tag")
--- Thriller (-//-)
--- Scifi
--- etc

After that inside movies set "tags" for every movie (one or multiple tags per movie).

1) Here is example how to get Some Horror-Thriller inside your category Thriller's:
 

// on page "Thriller"

$search_term = $sanitizer->text($page->title); // thriller
$items = $pages->find("tags.title%=$search_term, sort=-sort"); // here you can add more precious filters...

 

2) You have some "Tags Cloud" widget (eg. in some sidebar)

<?php
// tags cloud
$tags = $pages->find("template=tag, include=all");// include=all because of hidden parent
?>
<div class="widget tags-cloud">  
    <?php foreach($tags as $tag): ?>
        <a href="<?php echo $tag->url;?>"><?php echo $tag->title;?></a>        
    <?php endforeach; ?>  
</div>

Maybe links inside "tags-cloud" are little confused, eg. "my-website.com/tags/horror" ??? but that's part will be more clear if you watch main part inside tag.php and tags.php templates (below):
 

// tag.php

$search_term = $sanitizer->text($page->title);
$items = $pages->find("tags.title%=$search_term, sort=-sort");
// tags.php

$tags = $pages->find("template=tag");
$items = $pages->find("tags=$tags, sort=tags.title");

regards

p.s. this principle is used here on some of my testing website (at sidebar column is tags-cloud widget)

2 people like this

Share this post


Link to post
Share on other sites

Thanks for the input. :)

I found another solution though, so I thought I'd share it.

$stories = $page->children("sort=-date");
$stories->append($pages->get("/thrillers/")->find("template=horrorthriller"));

It may not be optimal, but it's short and it works :P

1 person likes this

Share this post


Link to post
Share on other sites

Actually, I've managed to come up with a very elegant solution now, and would like to share it. :)

I've added a Page field (multiple options) to the template (for the pages that have lists of pages on them).

Then I added the following code to the template file:

$stories = $page->children();
if ($page->pageimport) { $stories->append($page->pageimport); }
$stories = $stories->sort("name");

pageimport is the name of the Page field I mentioned above.

Now you can select pages that you want to import and display as 'fake' children of other pages. :)

2 people like this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.