0

I need to add a class of 'project-img' to each anchor on this page: http://petekingston.com/portfolio/test/

My code (PHP loop) for this atm is:

<div id="container">
<?php foreach (simple_portfolio_media() as $media_item): ?>
    <div class="box lightGrey masonry masonry-brick">
        <?php   
            $class = ('') ? 'class="project-img"' : '';
            switch ($media_item['type']):
                case 'image':
                    $src = wp_get_attachment_image_src($media_item['value'], 'full');
                    echo "<a $class href=\"$src[0]\">" . wp_get_attachment_image($media_item['value']) . "</a>";
                    break;
                case 'youtube':
                    echo "<p>";
                    echo "<object width=\"480\" height=\"385\">";
                    echo "  <param name=\"movie\" value=\"http://www.youtube.com/" . $media_item['value'] . "&amp;hl=en_US&amp;fs=1\"/>";
                    echo "  <param name=\"allowFullScreen\" value=\"true\"/>";
                    echo "  <param name=\"allowscriptaccess\" value=\"always\"/>";
                    echo "  <embed src=\"http://www.youtube.com/v/" . $media_item['value'] . "&amp;hl=en_US&amp;fs=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"480\" height=\"385\"/>";
                    echo "</object>";
                    echo "</p>";
                    break;
                case 'snippet':
                    echo $media_item['value'];
                    break;
            endswitch;
        ?>
    </div><!-- END box -->
<?php endforeach; ?>
</div><!-- END container -->

However, it is not adding the class to the anchors. Any help would be much appreciated.

Thank you in advance!

Pete

2
  • What is $class = ('') ? 'class="project-img"' : ''; ? It is checking what empty string? Commented Apr 15, 2013 at 10:40
  • I am unfortunately no good with PHP and have tried to incorporate code from here: stackoverflow.com/questions/2913415/… Commented Apr 15, 2013 at 10:43

2 Answers 2

0

Since you are not using your tag with the same class anywhere else, only in case 'image', why do you need a condition to $class variable? Even more, why do you need declaration 'class="project-img"' to the variable? I would be using this way:

 $class = 'project-img';
 ...
 echo "<a class='$class' href=\"$src[0]\">" ...
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic, the class is so I can target it with fancybox and style it. Thank you very much!
0

Your comparison operator in your ternary test is wrong : use == instead of =.

$class = ($class == ('')) ? 'class="project-img"' : '';

I'm not sure of what you mean by ('') by the way!

1 Comment

Yeah, I have changed that with @Royal_BG 's code. I know next to nothing about PHP so none of this code is what I have written. Thank you for the quick response though. (Apologies for not explaining the problem more accurately)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.