无法使用字符串替换更改the_content中的<p>标记

Jos*_*shc 4 php wordpress

我正在努力获得一个简单的字符串替换工作在wordpress the_content函数.

<?php 

    $phrase = the_content();
    $find = '<p>';
    $replace = '<p style="text-align: left; font-family: Georgia, Times, serif; font-size: 14px; line-height: 22px; color: #1b3d52; font-weight: normal; margin: 15px 0px; font-style: italic;">';

    $newphrase = str_replace($find, $replace, $phrase);

    echo $newphrase;

?>
Run Code Online (Sandbox Code Playgroud)


它似乎<p>还在呼应.

代替 <p style="text-align: left; font-family: Georgia, Times, serif; font-size: 14px; line-height: 22px; color: #1b3d52; font-weight: normal; margin: 15px 0px; font-style: italic;">

max*_*max 7

您需要使用apply_filters('the_content')带有段落的Wordpress换行符.

<?php 

    $phrase = get_the_content();
    // This is where wordpress filters the content text and adds paragraphs
    $phrase = apply_filters('the_content', $phrase);
    $replace = '<p style="text-align: left; font-family: Georgia, Times, serif; font-size: 14px; line-height: 22px; color: #1b3d52; font-weight: normal; margin: 15px 0px; font-style: italic;">';

    echo str_replace('<p>', $replace, $phrase);

?>
Run Code Online (Sandbox Code Playgroud)

请参阅the_content的codex条目.它在替代使用部分.