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 ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I am trying to add a field that provides a dropdown menu but there are no Options or Selector(s) type available - see attached image of field types available.
      The following reference under the docs does not appear to be applicable any more:
      https://processwire.com/api/modules/select-options-fieldtype/
      I can't see how to achieve this. Any assistance would be appreciated.

    • By mjut
      Hello!
      I am trying to get some extra css-file into my admin templates. I managed to modify my admin.php to this:
      <?php namespace ProcessWire; require($config->paths->adminTemplates . 'controller.php'); echo "<link rel='stylesheet' type='text/css' href='" . $config->urls->templates . "css/admin.css'>"; By doing so, the css <link> is added to the very end of each parsed admin-html. (right AFTER the closing </body> tag.)
      That causes some trouble. e.g. the page tree is not displaying any more. Although, other pages are working - like the edit form of a page.
      My question: is there a correct way of adding my extra css to the admin area?
      Thanks for you help!
      Stephan
       
    • By karian
      I don't know why multiple instances (repeater_repeat_columns1, repeater_repeat_columns2, ...) of my repeater field are displayed inside Template field (see image).
      Is there a way to clean/reset it ?
       

    • By awebcreature
      Hello there
      I need a field with realtime percent calculation (without page save)  with values from other fields in admin template. I think about jQuery with onChange methods on corresponding fields but this is not clear for me how to do this in module. Any help with some advice or example will be highly appreciated! 
      Thanks in advance! 
       
    • By psy
      I'm combining two PW sites into one, Site A into Site B.
      At each step, I did it bit by bit as the 'all at once' approach failed.
       
      First, I exported all the fields from Site A and imported into Site B. Any field types not supported by import/export, eg FieldtypeOptions I manually recreated. All good.
      Next I exported all the templates from Site A and imported them into Site B and copied across their associated template files. All good.
      Finally I exported the pages I needed from Site A into Site B - again, bit by bit to ensure it all went smoothly.
      From the admin side, it all looked and worked perfectly.
      Front end was a totally different story. All existing pages in Site B worked as expected. NONE of the pages imported from Site A displayed. They all ended in a redirect loop with no errors in the PW logs or Tracy Debugger.
      After some trial-and-error, I finally got it working with:
      - create a new template in Site B admin with no associated template file and just a title field
      - import the fields from the imported Site A template into the newly created template (both on Site B)
      - copy the Site A php template file into a new file that matched the new PW Site B template name and save in Site B site/templates
      I can deal with the above workaround. Just curious to know if I did something wrong or if the template import/export feature is problematic?
       
      ### Solution:
      While the export/import was a slow process, turned out the front end redirecting issue was unrelated. For reasons unknown, all templates marked as HTTPS only were the ones redirecting, ie all templates from Site A. Finally solved it by changing the $config->https to true in site/config.php
      Now the pages display correctly as https whether the template forces the issue or not.