mike62

[RESOLVED] Echo a field from another page

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

Share this post


Link to post
Share on other sites

Hm, it should work that way and has nothing to do with your tabs.

Check the id (1006) again, maybe it's just a typo :) Is that page unpublished maybe?

 

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Yeah, you have the page id correct. Did you try echoing: $settingsPage->id like I suggested? If that works, check other fields on that page and see if somehow it's just the store_hours field.

  • Like 3

Share this post


Link to post
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. :/

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
1 minute ago, mike62 said:

I tried upgrading, unfortunately. See here if you want the whole sordid story.

Yeah Mike. I read that a while back :).

Share this post


Link to post
Share on other sites

I see - I think I'd still persist with fixing those spex issues - I bet it won't be too difficult, but I obviously won't mention it again :)

Share this post


Link to post
Share on other sites

I'm already well past the budgeted time on this project, and firmly in Good Enough, Move On territory. :) 

Share this post


Link to post
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

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Livius
      Hello fellow Process wire Developers!
      Today i have encountered an issue using the repeater where I can't find out how to solve it therefore i am asking for help.
      To be specific, I am trying to get the "file" field working properly in the repeater but i couldn't find a way on how to assign a property of a field that is being used by the repeater.
      download_pdf is the name of the field that i want to access properties from.
      What i wished i could do was something like $download->download_pdf.size or $download->download_pdf->name, something like that but obviously these didn't work
      Hope there is a solution and thanks in advance!
      Best wishes,
      Livius

    • By wheelmaker24
      Hi,
      I got a new idea which come in handy when creating ProcessWire pages with enclosed subsections. Currently within the template family settings there is a setting "Can this template be used for new pages?" with the options "Yes", "No" and "One". My proposal is to add a fourth option that is "Single Child". This would allow the template to be created only once within another page (meaning that there wouldn't be any siblings with the same template).
      To give you an example look at this page structure for a multi-site webpage:
      Newsroom root Newsroom for Country 1 News articles Article 1 Article 2 ... Tags Search 404 Newsroom for Country 2 News articles Tags Search 404 Newsroom for Country 3 ... The root template will automatically redirect to the country newsroom page according to the browser's locale settings. Within each country newsroom there should obviously only be one "Tags", "Search" and "404" template. The option mentioned above would enable me to do this.
      A work-around solution for my specific use-case would be to select the option "No" and automatically create the Country Newsroom's children via a hook that triggers when a new country newsroom page has been created. But I think that this fourth option could come in handy for other use-cases as well.
      What do you think?
    • By louisstephens
      I have a "strange" issue with my dev set up, that I just noticed today. I set up an image field named "dev_gallery" and checked the "use tags" option when setting it up. I went to add a couple image to the field, and noticed that the tags option is not displaying at all for the image. Currently, the description field is the only current field on image edit. Have I missed a basic setting to enable this?
       
      I am currently using PW 3.0.62 as well.
    • By modifiedcontent
      Another dumb question:
      Is there a field type for a text-based array?
      In my case I need to store an array of RSS feeds for SimplePie. Storing that list of links in a textarea - text formatted to look like an array - doesn't work with the script. It works with one link.
      The images field is an array. I assumed there would be an equivalent for URLs or text in general.
      Or is there another approach for these cases?
      Edit:
      I guess using the optional (module) Repeater field type is the best solution in this case? I have now a working solution based on that. Still curious about other ideas.