如何在函数中添加PHP代码。php WordPress

时间:2021-01-17 作者:omid chahardoli

<?php
//Insert ads after second paragraph of single post content.
add_filter( \'the_content\', \'prefix_insert_post_ads\' );
function prefix_insert_post_ads( $content ) {
     
    $ad_code = \'<div>Ads code goes here</div>\';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }
    return $content;
}
嗨,如何重新定位

<?php
 $a1 = get_field(\'fa1\', \'option\');
 if ($a1) : ?>
  <a href="<?php echo $a1[\'a1-l\']; ?>" target="_blank" rel="nofollow"><img src="<?php echo esc_url( $a1[\'a1-b\'][\'url\'] ); ?>"></a>
<?php endif; ?>
而不是<div>Ads code goes here</div> 在里面functions.php Wordpress?

1 个回复
最合适的回答,由SO网友:Q Studio 整理而成

也许这就是你的意思?

<?php
//Insert ads after second paragraph of single post content.
add_filter( \'the_content\', \'prefix_insert_post_ads\' );
function prefix_insert_post_ads( $content ) {
     
    $ad_code = \'<div>\'.sewp_381661_get_advert().\'</div>\';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }
    return $content;
}

// function to return ad markup / logic ##
function sewp_381661_get_advert(){

    $a1 = get_field(\'fa1\', \'option\');
    $string = \'\'; // define return var ##
    if ($a1) {

        $string = \'<a href="\'.$a1[\'a1-l\'].\'" target="_blank" rel="nofollow"><img src="\'.esc_url( $a1[\'a1-b\'][\'url\'] ).\'"></a>\';

    }

    // return string for echo ##
    return $string;

}