web-dev-qa-db-ja.com

サムネイルの相対リンクとHTMLの修正を投稿する

私はすべてのthe_post_thumbnail()からWebサイトのURLを削除して、それらが相対的になり、出力から属性を削除または追加するようにします。

これまでのところ、テーマのfunctions.phpに次のコードを追加しました。xcept第2部のサムネイル$htmlを変更する方法がわかりません。

ヘルプは大歓迎です

add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

function my_post_image_html( $html, $post_id, $post_image_id, $size ) {

    // Create relative url using pars_url in php
    $relURL = wp_get_attachment_url( $post_id ); // Get Post thumbnail Src instead of permalink
    $relURL = parse_url( $relURL );
    $relURL = $relURL['path'];

     //Pass the relURL to post_thumbnail_html modify the src attribute
    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . $imgsrc .$post_id . '</a>';

    return $html;
}
2
MonteCristo

これはかなり驚くべき機能ですが、仕事をします、それはあなたの投稿のサムネイルを相対的なURLに変えます。これを行う理由がわからない場合は、慎重に使用し、エラーチェックを追加することをお勧めします。

function wpse_83137_relative( $html, $post_id) {

      // get thumb src
      $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
      $post_thumbnail_id = get_post_thumbnail_id( $post_id );
      $urly = wp_get_attachment_image_src($post_thumbnail_id );

      //parse the url
      $parse_it = $urly[0];
      $parsed_url = parse_url($parse_it, PHP_URL_PATH);

      //add other stuff
      $some_thing = 'something else you want to add';

      //output relative link, add more stuff here if you want for inside html tags
      $img = '<img src="' . $parsed_url . '">';

      return $img . $some_thing;
    }

    add_filter( 'post_thumbnail_html', 'wpse_83137_relative', 10, 3 );
0
Wyck