Help me foreach $categories

Hello everyone i have a problem.
Im using shopkit plugin.
Problem is ;
I using this code in homepage <?php foreach($categories as $category): ?>
its works.
But when i using this code in any non categories page its gives error.
How can i use this code like;

<?php foreach((MainPage)$categories as $category): ?>

Will give me homepage categories
any help?

Not quite sure how $categories is defined, but if they are subpages:

$categories = page('home')->children()->visible();
foreach($categories as $category): ?>
  <!-- whatever goes here -->
<?php endforeach ?>

I got error :frowning:

Whoops \ Exception \ ErrorException (E_NOTICE)
Call to a member function visible() on boolean

Th error message means that the page collection does not exist, so page('home')->children() returns false. So please tell us: What are $categories? What is the name of the home page? Maybe post the structure of your /content directory.

Firstly im using shopkit thats my code;

<?php foreach($categories as $category): ?>	
<li class="menu-item">
        <a href="<?php echo $category->url() ?>" title="">
        <span><?php echo $category->title()->html() ?></span>
 <?php endforeach ?>

This $categories command takes all subpages in current page

Ok, if I understand it correctly, the categories are subpages of the shop page but you want to fetch these categories in another page?

You probably then have to exchange home with shop in the code above (not tested)

$categories = page('shop')->children()->visible();
foreach($categories as $category): ?>
  <!-- whatever goes here -->
<?php endforeach ?>
1 Like

Thank you its perfectly worked :slight_smile:
Thats what i need!