正在尝试对帖子内容使用筛选器。但是我如何在函数内部使IF工作呢。
<?php function my_the_content_filter($content) {
} add_filter( \'the_content\', \'my_the_content_filter\' ); ?>
所以我需要在里面加上这个。
<?php if (get_post_meta($post->ID, "heading_image", true)) : ?>
<img src="<?php $image_id = get_post_meta($post->ID, "heading_image", true);
$post_image_data = wp_get_attachment_image_src( $image_id, $size=\'full\' );
echo $post_image_data[0]; ?>" style="max-width:1000px;" />
<?php endif; ?>
我似乎无法正确格式化它。
SO网友:Shazzad
应该是这样的-
<?php function my_the_content_filter( $content )
{
if( get_post_meta( get_the_ID(), "heading_image", true) )
{
$image_id = get_post_meta( get_the_ID(), "heading_image", true);
$post_image_data = wp_get_attachment_image_src( $image_id, \'full\' ); ?>
// checks if you have got the source
if( isset($post_image_data[0]) )
{
// if image is show before the content
$content = \'<img src="\'. $post_image_data[0] .\'" style="max-width:1000px;" />\' . $content;
}
}
return $content;
}
add_filter( \'the_content\', \'my_the_content_filter\' ); ?>