Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Zachary grace
Zachary grace
6,682 Points

How do i make a portfolio picture link to another website in Wordpress?

How do i make a portfolio picture link to another website in Wordpress? I want to make it so that on my website when someone is looking at my portfolio when they click on the screenshot image it opens a new window and takes them to the client's site. How do I do this?

2 Answers

Andrew Chappell
Andrew Chappell
12,782 Points
<a href="http://examplesite.com" target="_blank">
   <img src="image.jpg" alt="Something about your image">
</a>

You could just hard code it in like that.

A better way would be to use a plugin like Advanced Custom Fields to create a field for the website in the WordPress backend. Then you can get that URL and store it in a variable, which would then allow you to loop through all the websites you've built and insert the URL dynamically.

For example,

<?php

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 $image = get_field('image');

$url = get_field('url'); 

$description = get_field('description');

    <a href="<?php echo $url;?>" target="_blank">
      <img src="<?php echo $image; ?>" alt="<?php echo $description; ?>">
    </a>

<?php endwhile; else : ?>

    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

<?php endif; ?>

The basic idea here is that the WordPress loops fetches all the posts you've done for your Portfolio custom post type then goes through the above code and dynamically inserts the data you've inputted in the WordPress backend.

Some resources:

http://www.advancedcustomfields.com/resources/image/

http://codex.wordpress.org/The_Loop

https://wordpress.org/plugins/custom-post-type-ui/

You should also check out Zach's WordPress courses if you haven't already. Hope that helped.

Hi Zachary, I would recommend you check out Working with Media in WordPress.