Post Thumbnail URL CPT
January 2, 2025 ยท AI Conversations
if ( has_post_thumbnail() ) {
$thumbnail_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );
echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="' . esc_attr( get_the_title() ) . '">';
} else {
echo 'No featured image set for this post.';
}
### Explanation:
1. **`has_post_thumbnail()`**: Checks if the current post has a featured image.
2. **`get_the_post_thumbnail_url()`**: Retrieves the URL of the featured image. The `full` parameter ensures that you get the full-size image URL.
3. **`get_the_ID()`**: Gets the ID of the current post.
4. **`esc_url()` and `esc_attr()`**: Escapes the URL and the title for safe output.
### Usage:
– Place the code inside the WordPress loop for your Custom Post Type template (e.g., `single-{cpt}.php`).
– Replace `{cpt}` with the slug of your Custom Post Type.
Let me know if you need further assistance!
$stylesheet_directory_uri = get_stylesheet_directory_uri();
echo $stylesheet_directory_uri;
### Explanation:
– **`get_stylesheet_directory_uri()`**: Retrieves the URL of the current theme's directory. If you're using a child theme, this will return the child theme's directory URL.
– It is different from `get_template_directory_uri()`, which always returns the URL of the parent theme's directory.
### Example Usage:
If you want to include a CSS file located in your theme's directory, you can use this function as follows:
echo '<link rel="stylesheet" href="' . esc_url( get_stylesheet_directory_uri() . '/style.css' ) . '">';
This will output the link to the `style.css` file in your theme's directory.
Let me know if you have specific use cases or further questions!