Jump to content

Select pages using specific template


JerryDi
 Share

Recommended Posts

Hi,

I am new to PW so please excuse me if this is a bit basic!

I have ~1000 golfer names using the player template. Other templates with results for various championships use the player name in a field called winner_name which is a Page Reference field linked to the player template title field.

When viewing the player page I want to show all related pages that use a specific template. Please can you advise me on the php syntax required?

Thanks

Jerry

 

Link to comment
Share on other sites

assuming your other template name is championship you could do this in your player.php template

$playerChampionships = $pages->find('template=championship, winner_name=$page->id');

foreach($playerChampionships as $championship) {
	// …
}
Link to comment
Share on other sites

Thank you for your reply. This is what I have inserted into the template:

<p>Below you can follow links to this player's tournament wins </p> 

<?php

$playerChampionships = $pages->find('template=championship_results, winner_name=§page->id');

foreach($playerChampionships as $championship) {
    echo "$championship <br>";

}

?>

It does not return any results, although the player I am testing it with has 20+ wins to his name. Have I done something wrong? Note..I am also a php novice!

thanks Jerry

Link to comment
Share on other sites

One further question if I may:

this works fine, but I now want to add winner_name [which is a PageRef field derived from the title field in a separate template, Players] to the string that is returned

I have tried this additional text [in bold as below], but it only returns the id number for the winner

How can I get it to return the title field from the Players template ie the full winner's name?

<?php

$playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}");

foreach($playerChampionships as $championship) {
echo "<li><a href='$championship->url'> $championship->title - $championship->winner_name</a></li>";

}
?>

thanks

Link to comment
Share on other sites

thanks for this da2,

I had tried this but it produced this: County Championship 2023 - 1442->title

I have reverted to this:          foreach($playerChampionships as $championship) {
echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name}</a></li>";
}

?>

This produces  County Championship 2023 - 1442

So it's getting the player page ID (1442) but not the title value. As a php newbie I am baffled.

Any further thoughts?

thanks

 

Link to comment
Share on other sites

6 minutes ago, JerryDi said:

I had tried this but it produced this: County Championship 2023 - 1442->title

@JerryDi Looks like you missed something when adding "->title". I suppose you wrote:

echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name}->title</a></li>";

instead of:

echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->title}</a></li>";

 

Link to comment
Share on other sites

Hi, I have tried both of these options and a few others but none of them work

The two you have shown above do not return the page ID, but when I remove ->title, it does return the page ID [1442 being the ID]:

 County Championship 1995 - 1442     

Many thanks

J

 

Link to comment
Share on other sites

2 minutes ago, ngrmm said:

@JerryDi could you paste in here the latest version of your code.

<?php

$playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}");

foreach($playerChampionships as $championship) {
echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name}</a></li>";

}
?>

Link to comment
Share on other sites

as @da² mentioned you missed adding the "->title"

$playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}");

foreach($playerChampionships as $championship) {
	echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->title}</a></li>";
}

 

Link to comment
Share on other sites

Then maybe winner_name is a multipagefield and you try this:
 

$playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}");

foreach($playerChampionships as $championship) {
	echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->first->title}</a></li>";
}

 

Link to comment
Share on other sites

Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhh yeah I understand, we are all forgetting something tiny but quite important: the property to use on User is "name" instead of "title". 😆

$playerChampionships = $pages->find("template=Championship_results, winner_home_club={$page->id}");

foreach($playerChampionships as $championship) {
    echo "<li><a href='$championship->url'> $championship->title - {$championship->winner_name->name}</a></li>";
}

Property "winner_name" should be renamed to "winner" since it's a User instance and not a string. 😉

  • Like 1
Link to comment
Share on other sites

On 11/1/2023 at 2:10 PM, JerryDi said:

I have ~1000 golfer names using the player template.

Reading that I understand you are not using the User template, isn't it? So the property is still "title" and not "name".

Could you post 2 screenshots from PW admin? One of the "player" template properties (the green bars), and one of the "player" page with ID 1442.

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