Aohor Arsalan
Aohor Arsalan

Reputation: 191

How can I add rel to anchor tags & id to thumbnail in WordPress

I'm creating a portfolio page. & I have to dynamically generated "id" for images[thumbnail] & "rel" for the anchor tags. so I can connect them. but I don't know how to do this.

here is my current porofolio code:

<div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;">
<?php $args = array( 'post_type' => 'portfolio', 'order' => 'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $extraLastClass = $loop->current_post + 1 === $loop->post_count ? ' main-image-porfolio-main' : '';?>
<?php the_post_thumbnail( "thumbnail", array( "class" => "main-image portfolio $extraLastClass" ) ); ?>
<?php endwhile; ?> 

<?php rewind_posts(); ?> 


    <div class="portfolio-box">
        <h5>Portfolio</h5>
        <ul class="item-list" id="portfolio-list">
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
        <?php endwhile; ?>              
        </ul>
    </div>

</div>

I've also added a screenshot for better understanding. enter image description here

Upvotes: 0

Views: 558

Answers (1)

vaso123
vaso123

Reputation: 12391

Just simple use your attributes array:

$attributes = array(
    "class" => "main-image portfolio " . $extraLastClass,
    "rel" => "whatever rel you want",
    'id' => 'whatevere id you want',
    'whatever attribute you want' => 'whatever value for that attribute'
);
the_post_thumbnail("thumbnail", $attributes);

EDIT

If you want to add it to your link, then just simple modify:

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

to

<li><a href="<?php the_permalink(); ?>" rel="<?php echo $post->ID; ?>" id="<?php echo $post->ID; ?>"><?php the_title(); ?></a>

where $post is your post object.

And as I see, (from your previous question) you are not looping through on the titles.

Upvotes: 1

Related Questions