1

我正在开发一个新的 Wordpress 主题;默认索引视图显示最近帖子的摘录。一些帖子将与文件下载有关,并包含图像、描述和指向所描述文件所在位置的链接。这些类型帖子的图像将通过链接锚定(其他类型的帖子可能包含未链接的图像)。

对于这些类型的帖子,我希望图像在摘录中显示时链接到其条目的完整帖子视图(single.php),但是对于相同的图像在显示为完整帖子视图的一部分时链接到外部下载链接.

我不确定我将如何做到这一点。任何帮助将不胜感激!

4

2 回答 2

1

因为我没有定义帖子缩略图,并且帖子可能包含也可能不包含多个图像,所以我最终这样做了:

我关闭了摘录中的标签(我有一个摘录插件启用了摘录中的标签),然后我在 function.php 文件中添加了以下内容:

function catch_that_image() {  
    global $post, $posts;  

    $first_img = '';  ob_start();  
    ob_end_clean();  

    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', 


    $post->post_content, $matches);  

    $first_img = $matches [1] [0];  
    if(empty($first_img)){ 
    //Defines a default image    
    $first_img = "/images/default.jpg";  }  
    return $first_img;}

然后我将以下内容添加到帖子标题和摘录/内容之间的主索引模板文件中:

<p><a href="<?php the_permalink() ?>" alt="<?php _e('Read full article', 'theme');?>" title="<?php _e('Read full article', 'theme');?>"><img src="<?php echo catch_that_image() ?>"></a></p>    

随机注释:它被包裹在一个

出于造型原因。

再次感谢您引导我朝着正确的方向前进。

于 2011-08-27T21:55:37.487 回答
0

如果您的主题在首页使用“the_excerpt()”,我认为您可以在functions.php 中添加一个过滤器,并使用正则表达式将链接href 从下载链接更改为永久链接。

就像是,

function replace_link($content) {
   if (is_home())
      return preg_replace('regular_expression', get_permalink(), $content);
   else
      return $content;
}
add_filter('the_excerpt', 'replace_link');

在不知道您的下载链接是什么样子的情况下,我无法创建实际的正则表达式

于 2011-07-09T23:34:50.110 回答