Jump to content

[RESOLVED] Echo a field from another page


mike62
 Share

Recommended Posts

I'm trying to echo a field from another page, in my footer. I have a Website Settings page (id 1006) with several fields for general site settings, like store hours (field is named store_hours). In the footer, I have this:

<?php $settingsPage = $pages->get(1006); 
	echo $settingsPage->store_hours;
?>

Shouldn't that output the contents of that field? Right now it's printing a "0" (zero) on the page.

Elsewhere in the same footer template file, I have this code and it works fine:

<?php $homepage = $pages->get(1); 
	echo $homepage->body;
?>

Does it have anything to do with the fact that the field is organized into one of these tabs, at the top of the page editor?

(disclaimer: I'm new to PW, and have inherited this site from another developer; that's why I don't know how or why some of this stuff is set up the way it is).

Thanks!

Screen Shot 2017-11-27 at 12.51.31 PM.png

Link to comment
Share on other sites

18 minutes ago, maxf5 said:

Is that page unpublished maybe?

That won't matter because he is using $pages->get() rather than $pages->find()

I think you do need to confirm 1006 and if that is correct, try $settingsPage->id to make sure that works - that way you can narrow down if it's a problem with the page or the store_hours field.

  • Like 1
Link to comment
Share on other sites

Just now, adrian said:

That won't matter because he is using $pages->get() rather than $pages->find()

Actually, I do wonder about that - what version of PW is this? Maybe the behavior of get has changed since the early days, but I don't think so.

Link to comment
Share on other sites

It's PW v 2.3. I checked the publish settings, and it was set to "Hidden: Excluded from lists and searches". I tried unchecking that box but I still get a zero. The id for the page is right in the URL when you're editing the page, right?

This is the URL when I edit that page:

/admin/page/edit/?id=1006&s=1 

Yeah, it's really weird, because like I said, I'm already using the exact same technique to echo a field from the homepage, but it's not working for this other page. :S

Link to comment
Share on other sites

Thanks @adrian! That helped me track the problem down. Turns out there are two fields on this site for store hours, for some reason, and the one I needed was store_hours_content, not store_hours. :S

I wish there was a way to see the field name when you're editing the page itself, rather than having to dig through the Templates and Fields areas of the backend.

Anyway, chalk this one up to user error. :/

Link to comment
Share on other sites

9 minutes ago, mike62 said:

I wish there was a way to see the field name when you're editing the page itself

Two options:

1) Admin On Steroids gives a tooltip to show field names and link to edit them:

5a1efc42b74e3_ScreenShot2017-11-29at10_27_33AM.png.ce86d4302fac4927387612c8673f1633.png

 

2) Tracy shows a list of fields (and their values and settings for the page being edited. This is available from the Request Info Panel

5a1efc8988700_ScreenShot2017-11-29at10_29_12AM.png.b9b5f669cfb400e690efaa010f9e3299.png

  • Like 2
Link to comment
Share on other sites

Cool thanks! I also just discovered that I could have just been using: 

echo $settings->store_hours_content;

without even needing to use

$pages->get(1006);

Is there some way that the Website Settings page (1006) has been defined as some kind of global $settings variable?

Link to comment
Share on other sites

4 minutes ago, mike62 said:

Is there some way that the Website Settings page (1006) has been defined as some kind of global $settings variable?

Yep!

Obviously you can search the code files under site, but also if you turn on the Template Resources panel in Tracy Debugger it will show you where it was defined.

Just now, kongondo said:

Really??...on PW 2.3? :P

Yeah, sorry - good point!

@mike62 - you really should upgrade this site!

Link to comment
Share on other sites

1 hour ago, mike62 said:

I wish there was a way to see the field name when you're editing the page itself

Another option: set $config->debug = true in /site/config.php (which is a good idea in general while developing a site) and then hover the inputfield collapse/expand icon in Page Edit.

2017-11-30_090003.png.2128d1215faf5cc0739b48cbb9263230.png

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