1

私がやりたいのは、カスタムフィールドコンテンツ(各投稿のカスタムフィールドの値に挿入される動的リンクを持つボタン)を、the_contentの直後でプラグインの前に出力することです。

カスタムフィールドのコードは次のとおりです。

<div class="button">
  <a href="<?php echo get_post_meta($post->ID, 'Button', true); ?>">
    <img src="<?php echo get_template_directory_uri() . '/images/button.png'; ?>" alt="link" />
  </a>
</div>

ワードプレスのコーデックスで、私が欲しいものに似たものを得るために、フィルターをthe_contentに適用する方法のこの例も見つけました。これはコードです:

add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if ( is_single() )
    // Add image to the beginning of each page
    $content = sprintf(
        '<img class="post-icon" src="%s/images/post_icon.png" alt="Post icon" title=""/>%s',
        get_bloginfo( 'stylesheet_directory' ),
        $content
    );
// Returns the content.
return $content;
}

問題は、PHPがわからないことと、特定のケースに適用するために上記のコードをどのように編集すればよいかわからないことです。

私はそれを少し変更し、ボタンをリストすることができましたが、the_contentの前で、カスタムフィールドを有効にするPHPがありませんでした。

add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {

if ( is_single() )
    // Add button to the end of each page
    $content = sprintf(
        '<img class="button-link" src="%s/images/button.png" alt="Link" title=""/>%s',
        get_bloginfo( 'stylesheet_directory' ),
        $content
    );
// Returns the content.
return $content;
}

ここで出力を見ることができます:http ://digitalmediaboard.com/?p = 6583(右上の「show-me」ボタンです)

4

2 に答える 2

2
$content .= sprintf(...); // will add the button right after content.

あなたの例では

// Add button to the end of each page
$content = sprintf(
    '<img class="button-link" src="%s/images/button.png" alt="Link" title=""/>%s',
    get_bloginfo( 'stylesheet_directory' ),
    $content
);

に変更します

$lnk=get_bloginfo( 'stylesheet_directory' );
$content .= '<img class="button-link" src=$lnk."/images/button.png" alt="Link" title=""/>';

コンテンツの直後に新しいコンテンツ/ボタンを追加します。cssまた、コンテンツ内/コンテンツ後の必要に応じて、そのボタンを配置するためのスタイルを追加する必要があります。

index.phpコンテンツの直後に、質問で提供したコードを簡単に編集して追加できると思います。

アップデート:

add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
    if ( is_single() )
    {
        global $post;
        $imgLnk=get_bloginfo( 'stylesheet_directory' );
        $pgLnk=get_post_meta($post->ID, 'Button', true);
        $content .= '<a href="'.$pgLnk.'"><img class="button-link" src=$lnk."/images/button.png" alt="Link" title=""/></a>';
    }
    return $content;
}
于 2012-04-15T18:59:38.077 に答える
0

上記のコードを少し変更しただけです。以前の「img」属性は、Safariで「画像が見つかりません」アイコン(疑問符)を出力していました。'img'を'span'に置き換え、CSSの背景として画像を追加しました。

add_filter( 'the_content', 'my_the_content_filter', 0 );
function my_the_content_filter( $content ) {
    if ( is_single() )
    {
        global $post;
        $pgLnk=get_post_meta($post->ID, 'Button', true);
        $content .= '<div id="button-link"><a href="'.$pgLnk.'" target="_blank"><span class="button-link"></span></a></div>';
    }
    return $content;
}
于 2012-04-21T09:19:57.160 に答える