Jump to content

Always call/open the first found child url instead of parent url


Bacelo
 Share

Recommended Posts

Hello,

I wonder if it's possible to always call/open first child url instead of parent url.

F.e. (current situation):

- Parent 1 /parent-1-url

  - Child 1 /parent-1-url/child-1-url

  - Child 2 /parent-1-url/child-2-url

  - Child 3 /parent-1-url/child-3-url

F.e. (how I would like to have it):

- Parent 1 /parent-1-url/child-1-url

  - Child 1 /parent-1-url/child-1-url

  - Child 2 /parent-1-url/child-2-url

  - Child 3 /parent-1-url/child-3-url

Any ideas / help are welcome. :rolleyes:

Regards,

Bacelo

Link to comment
Share on other sites

Do you mean some form of 'first-child-redirect'? So when you visit Parent 1 you are automatically redirected to Child 1. Put this on your Parent 1 template file.

<?php
/**
 * Template: First Child redirect
 *
 */

if($page->numChildren) $session->redirect($page->child()->url);

taken from:

https://processwire-recipes.com/recipes/quick-first-child-redirect/

  • Like 2
Link to comment
Share on other sites

Thanks SiNNuT, that comes close but I have no idea how to set it up. :huh:

I Have the following code:

      <?php
	  $link_pages = $pages->get("/")->children(); // These are your top level pages

	  foreach ($link_pages as $lp) {
  	  if ($lp->name != 'categories') {
    	  echo "<li>" . "<a href='{$lp->url}'>" . $lp->title . "</a>";
  	  }

      if (count($lp->children())) {
        echo "<ul>";
        
        foreach($pages->find("template=category") as $category) {
          if (count($lp->children("template=gallery, category=" . $category))) {
            echo "<li><a href='{HERE I NEED THE LINK OF THE VERY FIRST FOUND CATEGORY - VALID FOR ALL LINKS!}'>{$category->title}</a></li>";
          }
          if (count($lp->children("template=basic-page, category=" . $category))) {
            echo "<li><a href='{$lp->url}'>{$category->title}</a></li>";
          }
        }
    	  echo "</ul>";
  	  }
	  }
	  ?>

Regards,
Bacelo

Link to comment
Share on other sites

Okay, both of you many thanks for your help ^-^

So where you able to implement it to what you needed? Because i could not quite get the intention from the code you posted.

Link to comment
Share on other sites

So where you able to implement it to what you needed? Because i could not quite get the intention from the code you posted.

:( Nope, not yet ... still trying to figure it out ...

Well, I try to discribe the code I posted:

That's how my navigation is:

Parent 1 (Child 1 | Child 2 | ...)

Parent 2 (Child 1 | Child 2 | ...)

Would like to change it to:

Parent 1 -> has link of its Child 1 (Child 1 | Child 2 | ...)

Parent 2 -> has link of Child 1 (Child 1 | Child 2 | ...)

etc.

The children are shown on hover and I would like to set the link of the parents to the link of their very first child.

In my case the children are categories. So, if there is a category found -> link to first one found.

Link to comment
Share on other sites

Thank you LostKobrakai.

I'm aware of both possibilities. First idea was to change the navigation, but then SiNNuT came up with the idea with the first child redirect snippet.

This should work, the only thing is, that my childs in the navigation are categories and not the "real childs". The childs are filtered by the categories.

So, I need to find a way how to change

if($page->numChildren) $session->redirect($page->child()->url);

to fit with my category-setup.

Right now, when I try the code  line, of ccourse the real first child gets called and this works. But I need to forward to the first found filter-category so all childs with the first found filter-category are called.

??? hmmm...

Link to comment
Share on other sites

Like you would do it on any other occasion:

// category is the current page
$gallery = $pages->get("parent=/, template=gallery, category=" . $page);

// $pages->get() returns a NullPage if it doesn't find anything,
// therefore the check for the id.
if($gallery->id) $session->redirect($gallery->url);
  • Like 1
Link to comment
Share on other sites

Thank you, LostKobraKai :) .

I think I'm getting a bit closer, but as I'm still a newbie to processwire (and also not a php programmer), I'm doing not easy to comprehend/understand and get it done.

<?php
foreach($pages->find("template=category") as $category) {
	if (count($page->children("template=gallery, category=" . $category))) {
		$catgal = $category->name;
		echo $catgal;
		
// category is the current page
//$catgal = $pages->get("template=category, category=" . $page);

// $pages->get() returns a NullPage if it doesn't find anything,
// therefore the check for the id.
// if($catgal->id) $session->redirect($catgal->url);
}
}
?>

So I included (for testing) to checked with

		$catgal = $category->name;
		echo $catgal;

to see, if there are children found with assigned categories below the parent-page and it works - so I get all the existing categories.

-> But now, I need to redirect the parent to the very first found category (only of that parent) so that it's children assigned to that category are shown.

Maybe I should better explain my backend page setup:

- parent 1 (template "main") -> want to redirect to the first found/existing category

  - Child 1.1 (template "gallery")

  ...

- parent 2 (template "main") -> want to redirect to the first found/existing category

  - Child 2.1 (template "gallery")

  ...

- Categories (hidden, template "structure")

  - Category 1 (template "category")

  ...

I configured a field "category" of type page (which I included in the gallery template) - so within an child page I can select the categories.

Sorry, If I might explain everything to complicated (actually, now I guess my topic is named wrong) :-[

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...