If you’ve been following along with this little series at all, you now that over the past several posts, we’ve looked at adding in custom meta boxes to be able to input a URL for our link post format. And we looked at how to split out our post formats to make it easier to edit existing ones, and add new ones. Today we will make use of the custom meta box, by actually taking that URL you can input to the meta box from your post screen and applying it to the title of your post, so that rather than the title linking to the articles permalink, it will link to the original article you are referencing.
Now the way I’m going to do this today is by making a new format-link.php template, which we set up in yesterday’s article. This is a minor edit, so you don’t necessarily have to do this if you don’t like the idea of all these new templates. You could just make use of a conditional statement. It would be easy to copy all of twenty ten’s loop.php to your child theme and then make a conditional for the link around the title. The conditional would just say something like if post format equals link, use the meta data, else use the permalink. I’m not going to cover that here, but if you would like an example of that drop a comment here, and I will show that code.
I’m going to pick up from yesterday and fill out our currently blank format-link.php. Basically the first thing I did was to copy all the code from our format-standard.php template and paste it into format-link.php template. You can code things by hand of course if you like. I wanted the basic layout to be the same, I just want to add in the link to the title, and remove a few extra things. So let’s take a look.
The first and most important reason for this template is the actual link. I know that I can get the meta data associated with the meta box we previously set up by using:
get_post_meta($post->ID, "_vetitleurl", true)
So now what I’d like to do is to take the existing title code from the template:
<!-- THIS IS THE EXISTING TITLE CODE --> <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
And wrap it in the link we will input from our custom meta box. But I also want sort of a fail-safe in there. I’m trying to think of future posts. Maybe I will make a post using the link post-format in the future, and that post will be full of a list of links or something. And I won’t be referencing any one specific article. In that case I wouldn’t want my article’s title to link anywhere but to the full post. So what I will do is put in a conditional. I’m going to tell WordPress that IF I put in a URL in the Link URL post meta box, to wrap the title with that link. If I don’t put in a URL, then link the title to the article’s permalink (the normal WordPress behaviour). Here’s the full code for our title now in format-link.php:
<?php if( get_post_meta($post->ID, "_vetitleurl", true) ): ?> <h2 class="entry-title"><a href="<?php echo get_post_meta($post->ID, "_vetitleurl", true); ?>" rel="bookmark" title="Link to <?php echo get_post_meta($post->ID, "_vetitleurl", true); ?>"><?php the_title(); ?></a></h2> <?php else : ?> <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2> <?php endif; ?>
That’s it really. That would be the only major change I made for the post format. I did go through and remove some of the data from the bottom of the post. I took out tags and categories from the index view of the link format. It’s up to you whether or not you do that. You can add or delete any further info you would like. Make it your own! Here is the full code now from my format-link.php for your viewing pleasure:
<?php if( get_post_meta($post->ID, "_vetitleurl", true) ): ?> <h2 class="entry-title"><a href="<?php echo get_post_meta($post->ID, "_vetitleurl", true); ?>" rel="bookmark" title="Link to <?php echo get_post_meta($post->ID, "_vetitleurl", true); ?>"><?php the_title(); ?></a></h2> <?php else : ?> <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2> <?php endif; ?> <div class="entry-meta"> <?php twentyten_posted_on(); ?> </div><!-- .entry-meta --> <?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?> <div class="entry-summary"> <?php the_excerpt(); ?> </div><!-- .entry-summary --> <?php else : ?> <div class="entry-content"> <?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) ); ?> <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?> </div><!-- .entry-content --> <?php endif; ?> <div class="entry-utility"> <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span> <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?> </div><!-- .entry-utility -->
And of course you can use some css to style this. On VoodooPress, I pretty much add in css boxes for all post formats except standard. I like to make them all stand out a little. I just have a little CSS3 fun. Give it a box, pad it a little, and use some transparency and text shadows, along with rounded corners, and box shadows. We covered that all back here somewhat.
Now one thing to note. This only affects the links post format as viewed from your index page. One problem is that we have the url link here wrapping the title. But if people visit the single view of the post, the link will not be present. So people will have no real way of visiting the article we are linking to. No worries, tomorrow we will cover all of that. We’ll get a link to the referenced article added in to the single view. And we will do it my favourite way, entirely from functions.php.
You’ve got some real power now. You can add meta boxes to collect various data. You know how to use that data. And you know how to add in all of the available post formats. I say get to work! Register all of those post formats, and make up templates for each. You can really expand the power of your twenty ten child theme!
