动态图像链接取决于摘录/“单个帖子”在 WordPress 中查看

发布于 2024-11-19 13:00:31 字数 249 浏览 2 评论 0原文

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

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

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

I'm working on a new Wordpress theme; the default index view displays the excerpts of recent posts. Some posts will be regarding file downloads, and include an image, description, and link to the location where the described files are hosted. The images for these types of posts will be anchored with links(other types of posts may contain images that are not linked).

For these types of posts, I would like the images to link to their entry's full post views(single.php) when displayed in excerpts, but for the same images to link to an external download link when displayed as part of the full post view.

I'm not sure how exactly I would accomplish that.
Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-11-26 13:00:31

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

我关闭了摘录中的标签(我有一个摘录插件启用了摘录中的标签),然后我添加了将以下内容添加到 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>    

,它被包裹在 a 中

随机注释:由于样式原因

。再次感谢您引导我走向正确的方向。

Because I don't have post thumbnails defined, and since posts may or may not contain multiple images, I ended up doing it this way:

I turned off tags in excerpts(I had an excerpt plugin enabling tags in excerpt), then I added the following to the function.php file:

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;}

Then I added the following to the main index template file in between the posts title and the excerpt/content:

<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>    

Random note: It's wrapped in a

for styling reasons.

Thanx again for guiding me in the right direction.

木落 2024-11-26 13:00:31

如果您的主题在首页使用“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');

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

if your theme uses 'the_excerpt()' for the front page, I think you can add a filter in functions.php, and with a regexp change the link href from the download link to the permalink.

something like,

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

I can't create an actual regular expression without knowing what your download link looks like

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文