Php 缩略图到外部链接和缩略图到帖子

Php 缩略图到外部链接和缩略图到帖子,php,image,thumbnails,wordpress,Php,Image,Thumbnails,Wordpress,以下是我想要完成的 目标A:我想将index.php中的每个缩略图链接到他们的帖子。 目标B:我想为single.php中的每个缩略图定义一个指向外部网站的超链接 您可能会问,为什么我要为single.php使用缩略图?原因是我想要这个布局: 到目前为止,我了解到有3种显示图像的方法: 将图像和文本一起插入编辑器区域,但问题是我不能以不同的方式浮动图像和文本,因为帖子中的所有项目都分配了一个p标记-我错了吗 自定义字段应该可以完成任务,但这似乎不是最有效的方法——还是我错了 张贴缩略图应该是最

以下是我想要完成的

目标A:我想将index.php中的每个缩略图链接到他们的帖子。 目标B:我想为single.php中的每个缩略图定义一个指向外部网站的超链接

您可能会问,为什么我要为single.php使用缩略图?原因是我想要这个布局:

到目前为止,我了解到有3种显示图像的方法:

  • 将图像和文本一起插入编辑器区域,但问题是我不能以不同的方式浮动图像和文本,因为帖子中的所有项目都分配了一个p标记-我错了吗
  • 自定义字段应该可以完成任务,但这似乎不是最有效的方法——还是我错了
  • 张贴缩略图应该是最简单的方法,但请看下面我的问题
  • 我有实现目标A和B的代码,但它们只能单独工作。 换句话说,如果存在“代码2”,则“代码1”不起作用。 我如何解决这个问题?还是有更好的方法来实现我的目标

    代码1:使用自定义字段(single.php)将缩略图链接到外部网站

    
    
    代码2:将缩略图链接到帖子(functions.php)

    add_filter('post_缩略图_html','my_post_图像_html',10,3);
    函数my_post_image_html($html、$post_id、$post_image_id){
    $html='';
    返回$html;
    }
    
    is\u single()函数将帮助您实现所需功能。在functions.php中尝试下面的代码,并从single.php中删除其他代码

    function my_post_image_html( $html, $post_id, $post_image_id ) { 
    if ( is_single()) { 
        $name = get_post_meta($post_id, 'externalurl', true);
        if( $name ) {           
            $html = '<a href="'.$name.'" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
        }
        return $html;
    }
    else 
    {
        $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
        return $html;
    }
    }
    
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );   
    
    函数my_post_image_html($html,$post_id,$post_image_id){
    如果(is_single()){
    $name=get_post_meta($post_id,'externalurl',true);
    如果($name){
    $html='';
    }
    返回$html;
    }
    其他的
    {
    $html='';
    返回$html;
    }
    }
    添加过滤器('post\u缩略图\u html'、'my\u post\u图像\u html',10,3);
    
    天呐,这真管用!!!最初它不起作用,但在代码底部包含了这一行之后它就起作用了------添加过滤器('post\u thumboil\u html','my\u post\u image\u html',10,3)------谢谢你的帮助!!
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
    return $html;
    }
    
    function my_post_image_html( $html, $post_id, $post_image_id ) { 
    if ( is_single()) { 
        $name = get_post_meta($post_id, 'externalurl', true);
        if( $name ) {           
            $html = '<a href="'.$name.'" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
        }
        return $html;
    }
    else 
    {
        $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
        return $html;
    }
    }
    
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );