Jump to content

Multiple parents


Vikestart
 Share

Recommended Posts

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?

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

Link to comment
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)

  • Like 2
Link to comment
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

  • Like 1
Link to comment
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. :)

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Probably a little too late to the party, but if anyone else os looking for an easy fix, i've got one, and it works like a charm:

My issue was with getting a single product (each on its own page) to show up dynamically under different solutions i.e:

              In-Store (solution 1) would need product 1, product 2, product 3

              e-Commerce ( solution 2) would need  product 1, product 3

short of dupicating the pages and having them as direct children to the 2 ( or more) different solutions wasnt viable,

so, my work around was this, edit the template you're using for the pages that need to show up under more than one parent (product pages) and call it tag( or what ever you want to, doenst really matter as long as your php reflects this fields name)

the relational layout of the pages is important also:

|-solutions ( top parent)

|--solution 1 ( child 1)

|--solution 2 ( child 2)

|--solution 3 ( child 3)

|--All Products ( child 4, a container for all products)

|---product 1 (child 1 of child 4)

|---product 2 (child 1 of child 4)

|---product 3 (child 1 of child 4)

when assigning this child to the various parent you want, enter the page name as a tag seperated by commas, the php will allocate it to all the page tabs it finds.

i've just spent a good couple days trying to work out the logic behind this so forgive me if ive just sperted this information out, i hope it makes sence! feel free to ask if not

                        


foreach($pages->get("/solutions/all-products/")->children as $prod){
	$check = $prod->tag;
	if (strpos($check, ',') !== false) { #check if tag field has multiple tags by lookin gfor the seperationg value ','
	$newstr = explode(",",$check); #split into array
	foreach($newstr as $tag){ #Iterate through the tags
    	if($tag == $page->title){ # check if tag relates to a page
        	echo "<a href='{$prod->url}'><li><font color='white'>{$prod->title}<font></li></a>";
    	} else {
            #Do Nothing      
        }
    }
    } else {
    if($check == $page->title){ # Fthis if no multiple tags detected 
    	echo "<a href='{$prod->url}'><li><font color='white'>{$prod->title}<font></li></a>";
    } else {
        #Do Nothing
	}
 }

I know its a super specific example, but you may be able to modify this for a differnet use!

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...