I'm developing a bio section for a client. Every bio is linked to another bio creating a chain of bios. The order of this chain should be random. The schema would be as following:

Bios: Bio a, Bio b, Bio c, Bio d

Links: a-b, a-c, a-d, b-a, b-c, b-d, c-a, c-b, c-d, d-a, d-b, d-c

One possible example would be:

"Anna is born in England..." (Bio a) "Anna met Bob during summer..." (Link a-b) "Bob works a taxi-driver..." (Bio b) "On this evening Bob met Charles..." (Link b-c) "Charles ..." (Bio c) and so on..

This schema generates a ton of possibilities. Currently there are 4 bios, but later on there will be probably 10. I know how to hard-code this, but it's a ton of if and elseif and not really flexible.

I'm flexible regarding the entry and field side, what ever fits best.

How would you code this? Any help appreciated!


Here how it should look in the end: Bio Chain Example 1

When you navigate to "Biography" it generates a bio chain, if you click "shuffle" it generates a new one. I made different approaches so far, but all seem to involve to much code.

Here is my latest:

// set all bios & pick one randomly, assume it's Luka's 
{% set allBios = craft.entries.section('biography') %}
{% set bioA = allBios.order('RAND()').limit(1).first() %} 

// Chose a random link connecting Luka to another member
// every bio has matrix fields holding the bio link texts
{% set linkA = bioA.biographyLink.type("bioLink").order('RAND()').limit(1).first() %}

// every matrix bio link has one tag to identify what connection it is
{% set tags = linkA.linkTag.first() %}
{% for tag in tags %}

  // is it connecting Luka with Nemanja?
  {% if tag == "con-luka-nemi" %}
    // display bio Nemanja
    // pick random bio link Nemanja-?
    // display picked link

      // search for next bio based on the chosen link above
      {% if tag == "link-nemi-emma" %}
        // diplay bio Emma
        // pick random bio link Emma-?
        // display picked link

          // is it connecting Emma with Guillaume?
          {% if tag == "link-emma-guillaume" %}
            // display bio Guillaume
            // pick random bio link Guillaume
            // display picked link

You see the problem, this generates many ifs and is only 4 levels deep, at the end there will be 10 bios.

  • 1
    I think we need more information on how you will use this. By random, do you mean that it is different every time you look at it, or just that they can be hooked up in arbitrary ways? What is the output you want? Commented Feb 20, 2018 at 23:56


Your Answer

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