I’m building a blog where 95% of posts will be of “link” post format.
I’d like to specify the URL explicitly when adding a new “link” post.
In a separate field between the title and the content. (Not taken as a first link from the content as Twenty Thirteen does by default)
What is the best way to achieve it?
In twentythirteen theme, the post format link is shown using the
content-link.php
file. The part that displays the link is:So the link is shown using the
twentythirteen_get_link_url()
; this function uses the wp functionget_url_in_content()
to get the first link in the content and shows it if there is one, if not it shows the the post permalink.Unlikely, these functions have no filter to the result, so to change this behavior you have 2 options:
The suggested solution is the second. In
wp-content/themes
folder create a folder namedtwentythirteen-child
and inside it put 2 files. The first is thestyle.css
that can contain just the required content:Now go to your dashboard and find this new created child theme and activate it. Visiting the site you’ll not see any change.
Now copy the
content-link.php
file from the twentythirteen theme folder to your child theme folder. Find the code I posted above (should be lines 13-15).Replace with this:
Save it.
Now you can add an hidden custom field
'_my_custom_link'
to the post and this will be used as url for the post.Being a hidden field (the key starts with
'_'
) normally you have added a metabox for it, but once you want display after the title in the post edit screen you can use'edit_form_after_title'
action hook to display the field and ‘save_post’ hook to save it.When diplaying the field be sure that the post type is ‘post’, and the post format is empty (new post or no post format) or is equal to ‘link’.
So, add a
functions.php
file in the newly created child theme, and in this file add:Preview or what you’ll get:
Now that you get it works, you can customize
content-link.php
in your child theme as you want.When twentythirteen gets an update, you can update it with no fear: your changes are secure in the child theme.