0

I am new to Wordpress dev, and I have a page that loops through some custom fields(repeater) of people.

I'd like each subfield link to go to another page that has more info from that person on the previous page(same field group data). Is there a dynamic way to do this or do I need to create a new page for each person to show more details on link click?

8
  • I'm not sure I understand your question, maybe you could provide some more details (or an example)?
    – Pim
    Oct 22, 2018 at 20:08
  • @Pim For example, I have a page and I'm using acf field group that holds data related to each person. I'd like to have a link under that person (on the initial page) that goes to a new page that shows more information about that person. So the field group contains the person name, age, hobbies, and career. On the Initial page it will show name and age, and a link saying something like 'read more' that will go to another page that has more of the person info from the field group - hobbies and career.
    – BluLotus
    Oct 22, 2018 at 20:21
  • I know I can make a page for each person but I was wondering if there is a way to do this without hardcoding a page for each person as more people can be added.
    – BluLotus
    Oct 22, 2018 at 20:22
  • I don't immediately see a way you could do this. Maybe you could have a "read more" that displays more info with jQuery on the same page? Not sure that's what you want to achieve by displaying this on a second page.
    – Pim
    Oct 22, 2018 at 20:26
  • ah ok, I would need to link to a new page. Thanks, for the suggestion though
    – BluLotus
    Oct 22, 2018 at 20:31

2 Answers 2

0

You would at least need to create one page where you can list all the info. In the php template for that page, you can then change that info based on the page you're linking from.

That would be the same link for all pages though (you could get around that with rewrite rules, but that's a different story).

Say you're linking from a page with ID = 123. Create a new page "The New Page".

In your link you can do something like:

<a href="/the-new-page?id=<?php the_ID(); ?>">Read More</a>

In the template file of the new page, you add something like:

$previous_page_id = $_GET['id'];

$value = get_field('my-field', $previous_page_id);

if($value){
  echo $value;
} else {
  echo 'Sorry, no information found.';
}
4
  • I see what you are saying, the page id would be arbitrary in my case since the page already contains all the person field data. In the loop that displays each link I need to pass a unique identifier for that set of data to then add a conditional in the-new-page template, right?
    – BluLotus
    Oct 22, 2018 at 21:31
  • You pass on the original page's ID in the link, so that you can retrieve the field data that's on that page.
    – Pim
    Oct 22, 2018 at 21:40
  • The page template already has a loop, since there's a different title, different content, etc... for each page. Simply add the code above with the_ID().
    – Pim
    Oct 22, 2018 at 21:42
  • sorry for the confusion, there's one page(id-123) that has all person data(person a, person b, person c). On the-new-page?id123 if I click the link under person a I'm passing in the array thats on page 123 I'm just looping through person a, person b, and person c instead of just showing person a data. Thats my understanding from the above code, unless I'm missing something? I'm wondering if I can pass the person object itself or some paramaters to the new page.
    – BluLotus
    Oct 22, 2018 at 22:46
0

I solved this by using this as my link, then doing a check on name to show the correct data in the new template:

 <a class="btn" href="<?php echo esc_url( add_query_arg( 'n', $name, site_url( '/person-info/' ) ) )?>">Read more</a>

And updating functions.php:

function add_custom_query_var( $vars ){
    $vars[] = "n";
    return $vars;
  }
  add_filter( 'query_vars', 'add_custom_query_var' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.