• I’ve been searching around but haven’t found a solution as yet.
    I’m working on creating a portfolio for a client, and there’s a lot of content that’s repetitive…

    For instance, right now the source of each post would be something like this:

    <div class="project">
        <a href="http://domain.tld/images/project-one-1.jpg" rel="shadowbox[project-one]"><img src="http://domain.tld/images/project-one.jpg" /></a>
        <a href="http://domain.tld/images/project-one-2.jpg" rel="shadowbox[project-one]" class="hidden"></a>
        <a href="http://domain.tld/images/project-one-3.jpg" rel="shadowbox[project-one]" class="hidden"></a>
    </div>
    
    <div class="project">
        <a href="http://domain.tld/images/project-two-1.jpg" rel="shadowbox[project-two]"><img src="http://domain.tld/images/project-two.jpg" /></a>
        <a href="http://domain.tld/images/project-two-2.jpg" rel="shadowbox[project-two]" class="hidden"></a>
        <a href="http://domain.tld/images/project-two-3.jpg" rel="shadowbox[project-two]" class="hidden"></a>
        <a href="http://domain.tld/images/project-two-4.jpg" rel="shadowbox[project-two]" class="hidden"></a>
    </div>
    
              ( and so on... )

    After adding around five projects, and messing it up a bit, I thought about using custom fields. As you can see, the only bit that really changes between each set of links in a div is the project-name. Within each div, the class name “hidden” is applied to all links after the first one. Also, the number of links in each div isn’t constant. Project One could have 3 links, while Project Two could have 10.

    I have three questions:

    • I created a new key named project and gave it the value of project-one. So far so good.
      For the second project, I used the same key project, and gave it the value of project-two.

      Here’s where I started facing problems. I know how to extract information if there’s just one unique value for each key. What does one do if the same key is repeated, with different values for each instance?

    • I guess I need to define the number of links for each project. I considered setting adding this number with the project key. For instance, the value for the first project would be project-one [separator] 2, where the name is followed by a separator (a comma? a bar?), and a number.

      How does one define a set of two related values within a single key?

    • The third problem may not be a WordPress-related issue, but I would be grateful if someone could sort it out for me. Within each set of links in a div, the first div is slightly different from the rest, which have a class name “hidden” applied to each of them. I guess I need to use foreach or a counter in there somewhere, but doing them all together, but I got all confused with all of the issues together.
Viewing 1 replies (of 1 total)
  • Thread Starter ousep

    (@ousep)

    6 hours, the forum says I took to resolve this…
    I guess I just needed to read up a bit more than I did.

    What I did was to add both the sanitized project name and the number of images to the custom field value, comma separated.

    I’m not sure if this is a hundred percent right/efficient, but it works as expected. Could someone give me an opinion on this code.

    <?php
    $project_values = get_post_custom_values('project');
    foreach ( $project_values as $oneproject ) { //start a project
    $project_array = explode(', ', $oneproject); // make an array of project name and number of images
    $project = $project_array[ 0 ]; //define $project as project name
    $images = $project_array[ 1 ]; //define $images as number of images per project
    echo "<div class='project'> <!-- start a project div -->
    <a href='http://domain.tld/images/$project-1.jpg' rel='shadowbox[$project]'><img src='http://domain.tld/images/$project.jpg' /></a> <!-- the first link is different from the rest -->
    ";
    
    for ($i = 2; $i <= $images; ++$i) { //loop to count images two onwards
    echo "<a href='http://domain.tld/images/$project-$i.jpg' rel='shadowbox[$project]' class='hidden'></a> <!-- consequent links are identical -->
    ";
    }
    	echo "</div> //end the project
    ";
    }?>

    This is going to save me hours. And make me popular with the ladies.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom fields question: one key, many values’ is closed to new replies.