WordPress Press This With Custom Post Types

A few days ago I posted a question on twitter asking about the use of the WordPress Press This feature with custom post types. As I use a custom post type on this site for my Bookmarks I wanted to be able to use the WordPress Press This feature to quickly add a bookmark to my site. After a response from @_mfields I have managed to create a solution.

Continue reading WordPress Press This With Custom Post Types

iMac Replacement at Last!

Having bought my 27″ iMac in early May it developed a fault whereby the screen froze every now and then and filled with mosaic dots. During this time the mouse moved but all other functionality ceased. As the iMac was under warranty I booked an appointment at the Genius bar at an Apple store and returned it to explain the problem.

It was clear that the ‘Genius’ didn’t know what was wrong but recommended a video card replacement. During the consultation it was also clear that the screen had a dead pixel and therefore a new LCD screen was required as well as a replacement to fix this screen flaw. What surprised me was the cost of replacing the video card and LCD display which is outlined in the image below. I would have thought that at this price (plus extra repairs – see below) it would have been more economical to replace the computer and fix this machine in their own time.

A week went by without hearing from Apple regarding the problem. Today I received a call from the Apple store indicating that they had noticed further problems every time they had added a part to the machine including a new logic board. Due to the increasing amount of problems with the computer the ‘genius’ on the phone indicated that they would be replacing the computer under warranty for the equivalent current model. I am pretty sure that this is much better than the model that failed so I am quite pleased actually. Hopefully I should be able to pick the computer up tomorrow and they even said they would migrate the data from the old machine.

Get WordPress Featured Image Information

I wrote a few days ago about getting the WordPress featured image URL. With this in mind I have also recently wanted to take this further on a couple of sites and get some of the data associated with the featured image. Specifically I wanted to be able to easily obtain the featured images URL, title and also the caption. With a little research and remembering that WordPress attachments are just another WordPress post type here is how I did this.

The fact that attachments (uploaded through the WordPress media library) are WordPress post types, just like posts, pages and links as well as any custom post types that you may create, makes accessing this information easy. It also means there is so much you can do with attachments, more than you may have first realised.

My goal here was to create my own template tags for the things that I wanted to do. My previous explained how this was done for the featured image URL and therefore here I set about doing something similar for the title and the caption. Below is the code to use in your functions.php file for getting the WordPress featured image title:


/**
* Returns the title of the featured image assigned to a post.
*
* @param integer $post_id The post ID to get the featured image title from.
* @return string The featured image title or an empty string if no
* title or post doesn't have featured image
*/
function wpmark_get_featured_image_title( $post_id = 0 ) {

// if we have no post id, use current post id.
global $post;
if ( 0 === $post_id ) {
$post_id = $post->ID;
}

// get the thumbnail id for this posts featured image.
$featured_image_id = get_post_thumbnail_id( $post_id );

// if we don't have a featured image.
if ( '' === $featured_image_id ) {
return '';
}

// return the title of the featured image.
return get_the_title( $featured_image_id );

}

Notice that this function uses the get_posts() function which may sound straight, but again remember that attachment are a post type and therefore this function can be used. The code for getting the caption is very much the same and is below, again place in your themes functions.php file.


/**
* Returns the title of the featured image assigned to a post.
*
* @param integer $post_id The post ID to get the featured image title from.
* @return string The featured image title or an empty string if no
* title or post doesn't have featured image
*/
function wpmark_get_featured_image_title( $post_id = 0 ) {

// if we have no post id, use current post id.
global $post;
if ( 0 === $post_id ) {
$post_id = $post->ID;
}

// get the thumbnail id for this posts featured image.
$featured_image_id = get_post_thumbnail_id( $post_id );

// if we don't have a featured image.
if ( '' === $featured_image_id ) {
return '';
}

// return the title of the featured image.
return get_the_title( $featured_image_id );

}

These two functions combined with the previous function published are very useful when creating a WordPress site, particularly one that requires showing post attachments in a very specific way.

Get WordPress Featured Image URL

I have recently been working on a client site that used the excellent WordPress Post Thumbnail feature (or Featured Image as it is now referred to in the WordPress dashboard). I came across a small problem in that when using the function the_post_thumbnail() to display the featured image of a post, it also returns the <img> tag as well as the URL of the image. Therefore I set about finding a way to solve this problem.

Having looked on the Internet for some answers I came across Lee Willis’ post on the same thing which outlines an excellent solution to the problem. One of the commenters asked where to put the code, and although I knew t his I thought it would be a good idea to package this up into a little PHP function that you could then call in your template files.

Place the following into your themes functions.php file:

To display the featured image URL you can echo mdw_featured_img_url( 'medium'); in your template file. You can replace ‘medium’ above for the size of the featured image URL that you want to return e.g. thumbnail, large or full. Even custom sizes work here that you had added using the add_image_size() function. Of course if you echo the function above it will display the URL on your page but you will probably want to use it in PHP one way or another.