I am making a site in wordpress to try to create my own theme. The issue is that when obtaining the path of the image, it also prints the ID of the post.
PHP code:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="entrada">
<?php
if ( has_post_thumbnail() ) {
//El wp_get_attachment_url me devuelve la imagen, y a su vez el ID se muestra
$image = wp_get_attachment_url( get_post_thumbnail_id( the_ID() ), 'full' );
?>
<div class="thumbnail" style="background-image: url(<?php echo $image; ?>);">
<a href="<?php the_permalink(); ?>"></a>
</div>
<?php
}
?>
<div class="entrada_info">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<small>Publicado el <?php the_time('j/m/Y'); ?> por <?php the_author_posts_link(); ?></small>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('No hay entradas .'); ?></p>
<?php endif; ?>
The objective was to obtain the featured image that was assigned to the post, and put it as a background in the div
thumbnail. So far so good, but as I said, it also prints the ID:
Yesterday I was looking for a way to get the images and what I found was with that piece of code, but I have never seen anyone saying that it also shows the id.
Any way to remove that ID?
I add
He background-image
returns it to me well:
I add
Image of the resulting code:
<a href="http://wp:9998/2017/01/24/faltan-75-dias-para-el-mobile-world-congress/" style="background-image: url(http://wp:9998/wp-content/uploads/2017/01/1482499331.png);"></a>
But the variable $image
I declare above keeps returning the id.
The function
the_ID()
you're using insideget_post_thumbnail_id( the_ID() )
, doesn't return the id, it paints it .Eg, what you are doing is equivalent to
get_post_thumbnail_id( echo get_the_ID() )
;You have to use
get_the_ID()
(which returns the value, and doesn't paint it):Documentation is your friend. :)
get_post_thumbnail_id()
gets the ID of the image. If you useget_the_post_thumbnail_url()
it, it returns the url and you can do echo with it. Much easier and more comfortable.