Cannot change <p> tag in the_content using replace string

I am trying to get a simple string replacement to work in the wordpress the_content function.

<?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; ?> 


It seems that this is something else repeating <p> .

Instead of <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;">

+4
source share
2 answers

You need to use apply_filters('the_content') to translate lines in Wordpress with paragraphs.

 <?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); ?> 

See the codex entry for the_content . Its in the alternative use section.

+7
source

the_content does not return content, but it echoes content.

If you want to get the contents in a variable, you should use

$ phrase = get_the_content ()

You should run this inside the loop just like the_content ()

+3
source

Source: https://habr.com/ru/post/1446213/


All Articles