Jump to content

Referencing "Select Option" field of child template


mtn
 Share

Recommended Posts

Layout:  Pages are created as children of Categories.  On the Pages template there is a "Select Option" field to select the particular subheading under which the page is to be displayed.

Objective:  The Category page is to be an index of child pages listed under various subheadings (as created by the Select Option field).

Question:  What is the syntax to reference that select option field of the child page template in order to display those subheadings and their respective child pages?

 

Presently, I have a $subcats variable containing the name of the appropriate select option field for the Category and the following code which simply displays the sorted child pages (sans subheadings):

 

$entries = $page->children("sort=$subcats, sort=page_headline");
foreach($entries as $entry) {
  echo "<div class='listing'>";
  echo "<img src='{$entry->page_main_image->url}' alt='{$entry->page_main_image->description}'>";
  echo "<h2>$entry->page_headline</h2>";
  echo "<p>$entry->page_summary    <a href='$entry->url'>Read on</a></p>";
  echo "</div>";
}

 

Edited by kongondo
code blocks
Link to comment
Share on other sites

I am not sure I follow. A page, whether a child or not, has access to all the fields contained in the template that that page uses. So, if $entry is a child page, $entry can reference any field in the template it uses. If the selection option field is called categories, $entry->categories references the option field. If it is a single select option field, then you can just echo that out. If it is a multiple selection option field, you need to loop through it, i.e. foreach($entry->categories as $cat).

I'm probably missing the obvious here, being late Saturday and all that :).

Link to comment
Share on other sites

Thank you kongondo & Robin S!  Use of your suggestions led to the following successful code (in case it may be helpful to others):

 

$entries = $page->children("sort=$subcats, sort=page_headline");
$subtitle = '';
foreach($entries as $entry) {
    if($entry->$subcats->title !== $subtitle) {
    $subtitle = $entry->$subcats->title;
    echo "<h3>$subtitle</h3>"
    }
echo "<div class='listing'>";
echo "<img src='{$entry->page_main_image->url}' alt='{$entry->page_main_image->description}'>";
echo "<h2>$entry->page_headline</h2>";
echo "<p>$entry->page_summary <a href='$entry->url'>Read on</a></p>";
echo "</div>";
}

 

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