+44 (0)785 715 1348
office@bogdans.co.uk

Currently Browsing: image

WordPress: get the featured image from the parent’s post 0

Posted on 11, September 2013

in Category Web development

Here’s what you need to do in order to get the image from the parent’s post //get the ancestors $familyTree = get_ancestors($post->ID,’page’); array_unshift( $familyTree, $post->ID ); //add the current page to the begining of the list //loop through the family tree until you find a result or exhaust the array $featuredImage = ”; foreach ( $familyTree as $family_postid ) { if ( has_post_thumbnail( $family_postid ) ) { $featuredImage = get_the_post_thumbnail( $family_postid, ‘full’ ); break; } } // if the page has a featured image then show it echo ( $featuredImage ? $featuredImage : “” )

Read More

Upload WordPress post thumb image with PHP 0

Posted on 26, July 2013

in Category Web development

Here’s what you need to use in order to upload the thumb automatically with PHP instead of manually from the backend. $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $filename = basename($image_url); if(wp_mkdir_p($upload_dir[‘path’])) $file = $upload_dir[‘path’] . ‘/’ . $filename; else $file = $upload_dir[‘basedir’] . ‘/’ . $filename; file_put_contents($file, $image_data); $wp_filetype = wp_check_filetype($filename, null ); $attachment = array( ‘post_mime_type’ => $wp_filetype[‘type’], ‘post_title’ => sanitize_file_name($filename), ‘post_content’ => ”, ‘post_status’ => ‘inherit’ ); $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); require_once(ABSPATH . ‘wp-admin/includes/image.php’); $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $post_id, $attach_id )

Read More