I am printing out the content of a specific post (103 in this case) with the following code:
<?php $post_id = 103; $queried_post = get_post($post_id); echo apply_filters('the_content',$queried_post->post_content); ?>
How can I make it exclude all the html tags like <p> and <br>s, and just show the text?
Thanks!
Use strip_tag(string) function to remove all html tags from string
echo strip_tags(apply_filters('the_content',$queried_post->post_content));
Related
I am trying this code:
<?php $img = get_post_meta($post->ID, "imagenes", $single = true);
echo "$img";
?>
Where $img= <img src="https://www.newtvguide.com/wp-content/uploads/2020/05/MiamiDMT-drk-purp-cover.jpg">
But on output, it's showing the element as text, all html come in texts like this: <img src="https://www.newtvguide.com/wp-content/uploads/2020/05/MiamiDMT-drk-purp-cover.jpg">It's coming whole html code as text, I want it html output ....... (You can see it here: https://www.ebmovies.icu/dragon-soldiers/
It's not coming in html, I want to show it as image and there will be a link on image.
So is there any way to output the code as html not just texts?
I'm trying to make a custom RSS feed with some alteration to the HTML content of each post.
Inside the template file rss-custom.php I have this:
<?php while (have_posts()) : the_post(); ?>
<?php echo processPostContent(); ?>
<?php endwhile; ?>
in functions.php, there are three replacements as follows :
function processPostContent() {
$post = get_post(get_the_ID());
$post_content = strval($post->post_content);
// replace h3 and h4 tags with h2
$post_content = preg_replace('/<(\/?)h((?![12])\d)/im', "<$1h2", $post_content);
// strip every attribute of <img> other than src
$post_content = preg_replace('/<img[^>]*(src="[^"]*")[^>]*>/im', "<img $1 />", $post_content);
// insert text after some closing tags
$post_content = preg_replace('/<\/(h2|p|figure)>/im', "</$1><p>Inserted</p>", $post_content);
return $post_content;
}
Then I get a strange result: out of 20 posts, only 7-8 of them will have been fully replaced. The remaining get the first two replacements but not the third one. Does anyone know why that is?
The solution, turns out, doesn't have anything to do with the loop nor preg_replace. Some posts' contents do not include any HTML tag, only plain text. That's why preg_replace didn't have any effect on them. When those contents are rendered in the RSS feed, however, <p> tags are automatically inserted. That's what led me to believe the third replacement was skipped.
First paragraph.
Second paragraph.
is turned to
<p>First paragraph.</p>
<p>Second paragraph.</p>
I'm customizing the layout of category blog view for my template and I need to display the article intro text without the tag. In my custom file "blogalternativelayout_item.php", I use:
<?php echo substr(($this->item->introtext),0,75); ?>
Anyway this renders the introtext as
<p>Lorem ipsum etc...</p>
How could I do to remove the paragraph tags?
Thanks in advance.
You can use php strip_tags() function. eg;
echo strip_tags($this->item->introtext);
the code above will strip all the html tags in introtext.
If you want to strip tags except tags, then you can put it like this:
echo strip_tags($this->item->introtext, "<a>");
You have to use regex to achieve this task
<?php
$text = substr(($this->item->introtext),0,75);
//get the contents inside <p> tag using this regex
$result = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $text);
echo $result;
?>
Thanks to both suggestions. I've created this code and working:
<?php
$desctrunc = substr(($this->item->introtext),0,75);
$desc = strip_tags($desctrunc);
echo $desc . '...';
?>
Thanks.
I use tinyMCE to input the blog content record. i write this:
This is my new entry
i format it with justify and heading 1
then i display it, but the result is:
<h1 style="text-align: center;">This is my new entry</h1>
how to show it correctly
Tinymce adds html tags to all the text that you enter in it. By default it adds <p> to all the text that you enter and save.You need to use the function strip_tags() in PHP to remove the html tags.
Example:
$a = "<h1 style="text-align: center;">This is my new entry</h1>"; //this data is have assigned for demo you can get it from any place
echo strip_tags($a);
$text = 'Test paragraph. Other text';
echo strip_tags($text);
echo "\n";
// Allow and
echo strip_tags($text, '');
//output :
//Test paragraph. Other text
//Test paragraph. Other text
I am calling in content in Wordpress via the below code. Eseentially, I am dividing the content of the post into three sections; 1. Before the tag, 2. After the tag and 3. Post gallery. The code I have so far works perfectly to get the content, however I am having an issue as all formatting tags (p in particular) are being stripped. Is there a way to retain these?
Thanks
<?php
// Fetch post content
$content = get_post_field( 'post_content', get_the_ID() );
// Get content parts
$content_parts = get_extended( $content );
?>
<p>
<?php echo $content_parts['main']; // Output content before <!--more--> ?>
</p>
<p class="read-more">
<?php echo strip_shortcodes($content_parts['extended']); // Output content after <!--more--> ?>
</p>
<button>Read More</button>
<?php $gallery = get_post_gallery_images( $post ); ?>
When you pull the post content using get_post_field, the autop filter is not applied:
http://codex.wordpress.org/Function_Reference/wpautop
You can apply all of the content filters yourself by adding this line after you set $content:
$content = apply_filters('the_content', $content);