Jump to content

null $pages object


davenorton
 Share

Recommended Posts

Hi All

I'm new to process wire and haven't touched php in about 8 years, however, from the lack of questions on the subject, I'm assuming this is not a common issue and more than likely my own doing.

I am using the default site template t get to know the product and have added a simple function to the _func.php code file. I am further, using the default pages:

image.png.3ded1ceb72262ed7f9e15297e372bfbd.png 

Now I have added the simple function:

function getHomePageName() {
    $homepageName = '';
    if($pages){
        $homepage = $pages->get('/');
        $homepageName = $homepage->name;
    }
    else
        $homepageName = 'Nothing found';
    return $homepageName;
}    

 

The $pages object  always null and the code                                                                                                                                                                                                                                                                                                                                                                                                           

Any help in determining why the the $pages object is null would be appreciated

 

Thanks

Dave N

Link to comment
Share on other sites

Hi @davenorton

Welcome to ProcessWire!

It is a scope problem: The $pages variable is not known inside your function by PHP. Use the global wire() function to get any API variable. At the beginning of your function, write this:

$pages = wire('pages');

And it should start to work.

Cheers

  • Like 2
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...