1
4

3 回答 3

5

这可以通过 CSS 完成,它支持所有主流浏览器,包括 IE7+。无需添加任何额外的类或<img>元素。

下面的 CSS 将针对 a 的任何链接.pdf,更改链接颜色,给它一些左填充并在创建的空间中应用背景图像:

a[href$=".pdf"] {
    background: url(images/pdf-icon.png) no-repeat 0 50%;
    padding-left: 25px;
    color: red;
}

演示:http: //jsfiddle.net/xuBpG/1/

于 2013-08-22T10:55:03.040 回答
1

尝试这个:

functions.php在您的文件中添加此代码。

add_filter('media_send_to_editor', 'my_filter_pdf', 20, 3);

function my_filter_pdf($html, $id) {
    $attachment = get_post($id); //fetching attachment by $id passed through

    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if ($mime_type == 'application/pdf') { //checking mime-type
        $src = wp_get_attachment_url( $id );

        $html = '<a href="'.$src.'" class="any-class-for-pdf-files">File</a>';
        return $html; // return new $html    
    }
        return $html;
}

.pdf当您将 pdf 文件插入帖子时,此功能将向锚标记文件添加一个类。

于 2013-08-22T10:38:55.750 回答
0

使用@tw16 的答案提供了一种简单且类似的方式来使用 Font Awesome 图标而不是静态图像。对于那些希望使用 Fontawesome 或其他支持 unicode 的图标库的人,试试这个:

/* MIME TYPE ICONS */
a[href$=".pdf"]::after {
  font-family: "fontawesome";
  content: "\0020\f1c1";
  color: inherit
}
a[href$=".doc"]::after {
  font-family: "fontawesome";
  content: "\0020\f1c2";
  color: inherit
}
a[href$=".xls"]::after {
  font-family: "fontawesome";
  content: "\0020\f1c3";
  color: inherit
}

当然,您可以使用::before::after伪类,具体取决于您希望图标的位置(左侧或右侧)。

感谢 tw16 对此的简单化方法。

于 2016-10-13T15:52:31.860 回答