I need to figure out how to use Custom Fields for PDF uploads. So, when a PDF is uploaded in a post, it will automatically place a PDF icon to the left of the link to the PDF file for download.
I need to figure out how to use Custom Fields for PDF uploads. So, when a PDF is uploaded in a post, it will automatically place a PDF icon to the left of the link to the PDF file for download.
You can use the following code to style pdf links with your stylesheet. it gives all pdf links in the post content a class of "pdf".
put this in your theme's functions.php
function add_pdf_class($content) {
$patterns = '/<a(.*?)href=["|\'](.*?)([\.pdf|\.PDF])["|\']/';
$replacements = '<a\1href="\2\3" class="pdf"';
return preg_replace($patterns, $replacements, $content);
}
add_filter('the_content', 'add_pdf_class');Thanks, so much.
Hmmm...found a glitch. I'm going back through old posts that I've imported in which the person entering post images left the default link to an image added. A feature I think should be disabled (image links to itself by default). Anyway, the PDF class is being added to the link for JPG post images.
Can you give us a link to a webpage which have the pdf class being assigned to images. On my testserver all other links including links to post thumbnails don't get the pdf class.
Took me a minute to find one. I cleaned up a lot of them by simply removing the anchor tags. Here you go: http://mstroopers.com/dev/2010/12/son-credits-success-trooper-dad-and-mom/
I changed the code a little bit, try it with this:
function add_pdf_class($content) {
$patterns = '/<a(.*?)href=["|\'](.*?)\.(pdf|PDF)["|\']/';
$replacements = '<a\1href="\2.\3" class="pdf"';
return preg_replace($patterns, $replacements, $content);
}
add_filter('the_content', 'add_pdf_class');Worked like a charm! Again, thanks. Really appreciate it.
This topic has been closed to new replies.