Jump to content

Using $pages in custom function


Vineet Sawant
 Share

Recommended Posts

Hello all,

So me being a noob can't figure out how can I use $pages->get or find in my custom function.

function printTitle($page_id){
     echo "Title of the page is ".$pages->get("id={$page_id}");
}

Above code is giving me error :

 Error: Call to a member function get() on a non-object 

From what I've read probably, I should be using $wire('pages')->get.

But this gives following error:

Error: Function name must be a string

Removing $ from wire solves above error but it doesn't print anything.

Any help would be very much appreciated.

Thanks a lot.

Link to comment
Share on other sites

Vineet - it's all about variable scope. $pages (or any of the other PW variables) can't exist inside a function like that.

You need to use: wire('pages')->get or define $pages = wire('pages') at the start of your function and then you can use: $pages.

Nevermind - sorry, apparently I didn't actually read your post :)

Looks like renobird's suggestion should be spot on!

Link to comment
Share on other sites

Vineet,

You are getting the page correctly, try adding title to the end.

echo wire('pages')->get("id={$page_id}")->title;


...and perhaps what onjegolders mentioned as well. I think omitted title should still output the page ID.

  • Like 1
Link to comment
Share on other sites

Thank you guys for quick reply, will try defining $pages so I have following code now. And it prints nothing :(

function printPage($page_id){
	$pages = wire('pages');
	echo "Page Title: ".$pages->get("id=$page_id")->title;
}
Edited by Vineet Sawant
Link to comment
Share on other sites

  • 11 months later...

I'm having the same issue.

This is my function (which is sit on another php file )to print archive post, similar as the wordpress archive widget

<?php
function archive () {
$out = "
<h3>Archive</h3>
";
$start    = new DateTime('2010-01-01');
//$start->modify('first day of this month');
$end      = new DateTime('2015-02-01');
//$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

$a = array();
foreach ($period as $dt) {
    array_push($a, $dt);
 }
$a = array_reverse($a);

foreach ($a as $dt) {
    $d = $dt->format("Y-m-d");
    $nextmonth = date('Y-m-d', strtotime('+1 month', strtotime($d)));
    $count = count($pages->find("template=post, post_date>=$d, post_date<$nextmonth"));
    if ($count > 0) {
        $out .= "<a href='{$pages->get('template=home')->url}" . date('Y', strtotime($d)) . "/" . date('m', strtotime($d)) . "'>" . date('M Y', strtotime($d)) . " (" . $count . ")" . "</a><br/>";
    }
    
}

return $out;
}

In a template file, the code will looks

<?

include('./archive.php');

$out = "This is content.";

$sidebar = archive();

$page->body .= $out;

include('./main.php');  // the master output file

I will get this error in the log

Error:     Call to a member function find() on a non-object (line 26 of /var/www/html/pw1/site/templates/archive.php)

  • Like 1
Link to comment
Share on other sites

It's not only the same error in the log, but you're also doing the same thing wrong. You can't use $pages in functions because of variable scope. ProcessWire makes these object automatically available for the scope of the templatefiles, but inside a function is a new scope, so you need to either redefine the variable itself or use a function to get the object. 

// Template scope

$id = 15; // simple variable, defined by you
$pages = $pages; // the pages object, defined automatically by processwire

function something(){ 
  // this is now a new variable scope 
  // neither $pages nor $id are available here.
  // the api variables are not a special global variable

  $pages = wire('pages');
  $pages->find("");
  // OR just
  wire('pages')->find("");
}

It's the same reason, why you can't use $pages in modules. $page/$pages and the other variables are just convenient to use in templates. Everywhere else you need to call them differently.

  • Like 8
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

×
×
  • Create New...