私がやりたいのは、カスタムフィールドコンテンツ(各投稿のカスタムフィールドの値に挿入される動的リンクを持つボタン)を、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」ボタンです)