Get image attachment to a post outside the loop?

try using get_attached_media function and your current $postID $images = get_attached_media( ‘image’, $postID ); this will return a WP_Post object. If you want to get the url, it’s located in the guid key of the object. Use a foreach loop to retrieve them all. foreach( $images as $image ){ $src[] = $image->guid; } To use … Read more

Remove attachment page for audio media type only

You must use this function wp_check_filetype to check what is your media type $filetype = wp_check_filetype(‘image.jpg’); echo $filetype[‘ext’]; // will output jpg then you can have something like this $file_url = wp_get_attachment_url( $file_id ); $filetype = wp_check_filetype( $file_url ); switch ($filetype) { case ‘image/jpeg’: case ‘image/png’: case ‘image/gif’: return // do whatever you want break; … Read more

WordPress Attachments with old images

To get all featured images belonging to a certain post type, you’d have to get all those posts and the loop through them to get the featured image. Like this: $query = new WP_Query( array( ‘post_type’ => ‘yourposttype’, ‘posts_per_page’=>-1, )); $thumb_list = array (); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); … Read more

Post Title to link to a PDF

I recommend using the plugin Advanced Custom Fields (Very popular plugin). It lets you create fields and manage their values very easily. This way you could just create a new field for file upload and then to pull its information like this- <?php $my_file = get_field(‘my_file’); if( $my_file ) { echo $my_file[‘url’]; echo $my_file[‘alt’]; echo … Read more

Find out last 7 days of upload images, last week uploaded image from Gallery and display them in different pages

Here I’ve written a function for you- function the_dramatist_get_post_by_time_range( $args=”” ) { $date_query = array(); if ( $args === ‘last 7 days’) { $date_query = array( ‘after’ => ‘1 week ago’ ); } else { $month_ini = new DateTime(“first day of last month”); $month_end = new DateTime(“last day of last month”); $date_query = array( array( … Read more

Is There A WordPress Hook for Accessing Attachments for A Particular Page?

Here’s one way, out of many, where we use the the_content filter and loop through the get_attached_media( $type ) to construct the list of attached media. We can e.g. use $type=”image” for images or $type = null for all mime types. add_filter( ‘the_content’, function( $content ) { // Nothing to do – target single posts … Read more

How to get $attachment->ID in custom functions

To get the attachment ID, you need to get the post attachments using get_posts() with the post_type being ‘attachment’ and the post_parent being the ID of the post you’re interested in getting the attachments from. namespace StackExchange\WordPress; function the_post( \WP_Post $post, \WP_Query $query ) { //* Get post attachments $attachments = \get_posts( [ ‘post_type’ => … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)