Wordpress do_shortcode() with post_content - wordpress

I want to make the title of any post from post content. So, I made the following in functions.php
function my_title($title)
{
global $post;
// Do whatever here with your title...
$content = $post->post_content;
print $content;
$title =$content. $post->post_title . ' | ' .
get_bloginfo('name');
return $title;
}
It prints shortcode inside post content but if i apply $content = do_shortcode( $content ); it does not produce the actual post content. When i applied $content = do_shortcode( $content ); website hanged. Let me know how to use $content = do_shortcode( $content ); inside this function so that title can be changed.

Can you try this with wp_filter_nohtml_kses. I guess your html o/p is causing the issue
$content = $post->post_content;
$content = wp_filter_nohtml_kses( $content ); // this wp_filter_nohtml_kses indicates strip_tags
$content = do_shortcode( $content );
or
echo do_shortcode(get_post_field('post_content', $post->id));

Related

Wordpress return incomplete post_content from get_post

I'm trying to render a page made by Elementor on my own custom page.
Using this code, I only get incomplete html string whose dom has no classname...
<?php
my_header();
$my_id = 1234;
$my_post = get_post($my_id);
$content = $my_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
my_footer();
?>

How can i get inverted coma instead of apostrophe in wordpress content frontend

I write inverted comma on admin tinymce editor but on frontend it displays an apostrophe when I use:
the_content();
But when I use
echo get_the_content();
it will display inverted comma.
And when I use
$content=get_the_content();
$content=apply_filters( 'the_content', $content );
echo $content = str_replace(']]>', ']]>', $content);
it again display apostrophe instead of inverted comma.
I try to replace apostrophe by comma using
str_replace("’", "'", $content);
It doesn't work. Any suggestions?
I have tried this one
<?php $content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace(']]>', ']]>', $content);
$content = html_entity_decode($content);//added extra code
echo $content = str_replace("’", "'", $content);
?>
adding
$content = html_entity_decode($content);
this code then it works for me.
But still didn't know why WordPress convert.

<!--more--> being ignored when WordPress content is displayed

I'm trying to write a plugin that will take page id's and return a preview of the page. Here's my code:
function page_preview($atts,$pageid = null) {
extract(shortcode_atts(array(
"pageid" => '0'
), $atts));
$the_query = new WP_Query( 'page_id=' . $pageid . '' );
global $more;
$more = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content=get_the_content();
}
}
return '<div class="callout">' .
'<h4>' .
$title .
'</h4>' .
$thumbnail .
$content . '<a href="' .
get_the_permalink($pageid) .
'">Continue reading</a></div>';
}
add_shortcode( 'pagepreview', 'page_preview' );
and it gets called on wpadmin editor like so:
[pagepreview pageid=11][pagepreview pageid=13][pagepreview pageid=8054]
i.e., that would display a page preview for each of those page ids.
The "more" doesn't work.
global $more;
$more = 0;
usually fixes this issue, but it's not in my case. Can anyone see what I"m doing wrong? Thanks.
You're getting the full content because
$content=get_the_content();
is not applying the_content() filters, and $more=0 must be on a line after $the_query->the_post(); inside the while loop. Change your while loop to this:
while ( $the_query->have_posts() ) {
$the_query->the_post();
$more=0;
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
}
See get_the_content and read more in pages on the WordPress Codex for where I sourced this.

Plugin's shortcode not working

Hi I have tried as the following
$my_postid = 12;//This is page id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
I have written shortcode in the page with id 12. It doesn't works. Please help!! !
You can use the get_shortcode_regex function to check the post content and isolate the shortcode. Replace my_shortcode with the actual identifier of the shortcode you're trying to get:
$content_post = get_post( 12 );
$content = apply_filters( 'the_content', $content_post->post_content );
$pattern = get_shortcode_regex();
preg_match( '/' . $pattern . '/s', $content, $matches );
if ( is_array( $matches ) && $matches[2] == 'my_shortcode' ) {
$shortcode = $matches[0];
echo do_shortcode( $shortcode );
}

wordpress theme excerpt function elimination-thao studios theme

I want to remove the excerpt function or render it functionless as i want all posts to be viewed in full of its content. I think the theme has some tracking to make sure the excerption is in that line, so it must exist.
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
I only have these 2 lines of codes which i know is related.
I dont know where the $limit come from, i tried to find on all theme related php,no findings.
Please help me. Thank you very muc.
Just find your template file (could be index.php) where you want to display the full content instead of excerpted content and replace the function the_excerpt() with the_content() inside the loop, i.e.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- do other stuff ... -->
the_content();
<?php endwhile; ?>
<?php endif; ?>
About the_content() and the loop.

Resources