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

Jake Bolton
Jake Bolton
1,956 Points

WP Attachments Loop

Hello,

I hope someone can help. I have an issue with an attachment loop in Wordpress.

The client has recently updated Wordpress and the following attachment loop has stopped working. I'm struggling to amend this to make it work with the current version.

<?php $attachments = attachments_get_attachments(); $total_attachments = count($attachments); ?> <?php if( $total_attachments > 0 ) : ?> <?php for ($i=0; $i < $total_attachments; $i++) : ?> <a href="<?=$attachments[$i]['location']?>" title="<?=$attachments[$i]['caption']?>" class="lightbox" rel="<?php the_ID(); ?>"><img src="<?=$attachments[$i]['location']?>" alt="<?=$attachments[$i]['caption']?>" class="attachment-thumbnail" /></a>

<?php endfor ?> <?php endif ?>

It seems that the media attachment is in sequence with the blog posts, so the latest post will feature the latest media upload.

Hope this makes sense.

Many thanks, Jake

Hello, Jake.

Where did this function came from?

attachments_get_attachments();

Also providing any results you have will be helpful.

3 Answers

Hi Jake.

The error says that attachments_get_attachments() function doesn't exist, Normally, It should be in you functions.php file.

And btw, updating WordPress core shouldn't touch your theme files, something else happened with your functions.php file, If you have any old backup you can copy and paste attachments_get_attachments() function from there.

Jake Bolton
Jake Bolton
1,956 Points

Hi Muhammad,

Not sure how you post code into a forum post but here's the loop:

<?php
$attachments = attachments_get_attachments();
$total_attachments = count($attachments);
?>
<?php if( $total_attachments > 0 ) : ?>
<?php for ($i=0; $i < $total_attachments; $i++) : ?>
<a href="<?=$attachments[$i]['location']?>" title="<?=$attachments[$i]['caption']?>" class="lightbox" rel="<?php the_ID(); ?>"><img src="<?=$attachments[$i]['location']?>" alt="<?=$attachments[$i]['caption']?>" class="attachment-thumbnail" /></a>

<?php endfor ?>
<?php endif ?>

For some reason this is creating a fatal error, it was originally done to work in a much older version of Wordpress. I'm guessing that when the client updated it automatically removed support for this function.

Hope that makes it a bit clearer.

Thanks for your response.

Thanks, Jake

Can you post that fatal error please?

Jake Bolton
Jake Bolton
1,956 Points

Hi Muhammad,

The fatal Error went like this:

"Fatal error: Call to undefined function attachments_get_attachments() in /Users/jakebolton/Documents/Work/sites/albamblog.com/wp-content/themes/albam/loop.php on line 30"

Thanks, Jake