Preventing WordPress excerpts from hiding videos - php

I'm using WordPress on my website and I want to limit the posts length on the front page with the_excerpt(); to about 100 words. However if I do another post that has a video will hide the video and I don't want that.
However if I use the_content(); and the <!--more--> tag in the post the Read More link has #more-## after it and causes it to be almost at the bottom of the page.

You can create your own function instead of excerpt
function get_new_excerpt(){
$excerpt = get_the_content();
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$new_excerpt = substr($excerpt, 0, 20);
return $new_excerpt;
}
Place this in functions.php
Now instead of the_excerpt, use
echo get_new_excerpt();

Related

How to strip shortcode in Wordpress HTML

I have a custom theme where when I upload an image with caption, the caption shortcode is shown on the page: [caption id="attachment_109"].... Using either of WP's default themes, I see no issue. On inspection, WP uses the_content(), link, so I need to do some stripping. I get my posts with get_page_by_path():
<?php
$post = get_page_by_path( $current_page->post_name, OBJECT, 'service' );
// I assume this would work
$content = the_content($post->post_content);
//Blank page:
echo $content;
Echoing $post->post_content shows the caption shortcode as mentioned above. How to get rid of if it? BTW, I need the caption values.
You can get the post content like this
$post = get_post(123); //pass the id of the post
$content = $post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
or
$content=apply_filters('the_content', get_post_field('post_content', 123)); //123 is the post id
after that just strip the shortcode also you can check if the post is having the shortcode in it or not by has_shortcode

Reduce woocommerce excerpt length?

So, i am currently working with the theme Neighborhood and it seems like when i choose to show description under the products it shows the whole thing.
Is there any way to limit the Excerpt length for that? I've tried some different codes that i've put into funtions.php and also tried to edit the short-description.php in the woocommerce folder to no avail.
i've tried this code
<?php $excerpt = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
echo substr($length,0, 10);
?>
Is it something that i am missing here i wonder?
Looks like you need a custom function
function get_the_twitter_excerpt(){
$excerpt = get_the_content();
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$the_str = substr($excerpt, 0, 20);
return $the_str;
}
you should be able to put the above into your functions and this
<?php echo 'Excerpt: '.get_the_twitter_excerpt(); ?>
into your loop.

Overriding Excerpt in WordPress with Advanced Custom Field

I'm new to WordPress and we have a form that people can fill out to submit posts onto our site.
We used Advanced Custom Fields plugin to fill out all the necessary information we needed. We have on field that is story_description.
I would like to make that story_description to use all the excerpt filters (length, etc) and save it under the post_excerpt in the wp_posts table.
How would I go about having that save in that table with the custom length and the other specific in the excerpt that are defined? I'm new to all the filters and actions.
Is there a way to just overwrite is and treat is like a normal excerpt when the post is saved?
I appreciate any responses.
Your first need to add the following to allow excerpt generation outside of the loop (this go in your theme functions.php):
function rw_trim_excerpt( $text='' )
{
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
return wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
add_filter('wp_trim_excerpt', 'rw_trim_excerpt');
This function comes from jlengstorf answer.
Then when you insert your post do the following:
wp_insert_post(array(
// ... other stuffs ...
post_excerpt => apply_filters('get_the_excerpt', $story_description)
));
This will filter your $story_description string with all the excerpt WP functions.

Remove everything from output except between <p> tags

I have a Wordpress site that outputs content from individual blog posts with the_content()
Blog posts all consist of two things, a small gallery and some text:
<div class="gallery"><img></img>Blah Blah</div>
<p>Text</p>
<p>Text</p>
I'd like to split the gallery and the text and output the gallery in a div on the left and the text in a div on the right like this:
<div id="left">GALLERY CONTENT</div>
<div id="right">TEXT CONNTENT</div>
I have tried to do this with strip_tags(the_content(), '<p>') but this does not - it continues to output everything including the gallery.
What is the correct way to do this?
It is not really clear to me what you really want to do , and on to of it , you included some (very little ) source code from output, but to get a real shot at answering you need to include the relative code from the template file.
( And +1 for understanding alone that you should NOT TOUCH YOUR CORE FILES )
Anyhow, I suspect that you only want to disable the auto P generated by wordrpess , so try
remove_filter('the_content', 'wpautop');
(add to functions.php in theme.)
alternatively , you could use
add_filter('use_default_gallery_style', '__return_false');
Which will just "reset" the gallery styling .
or even filter your own gallery styles , which allows you to target them better.
add_filter( 'gallery_style', 'my_own_gallery_style', 99 );
function my_own_gallery_style() {
return "<div class='gallery'>"; // put your own
}
If it does not produce the right output for you, please include more specifics and / or more code .
There are of course more advanced ways to handle this , but Without more info it is difficult to target .
For example you can create your own gallery style by removing the original shortcode function, and then adding your own, but those are a bit more advanced techniques.
// deactivate WordPress function
remove_shortcode('gallery', 'gallery_shortcode');
// activate your own own function
add_shortcode('gallery', 'my_own_gallery_shortcode');
// the own renamed function
function my_own_gallery_shortcode($attr) {
...
}
Now on the other hand , If you want to "catch" some parts of 'the_content' and display it in a loop in a different manner , you can always use a different technique, like described HERE on another answer .
You are using the_content which displays the content instead of returning it.
Change your code to
strip_tags(get_the_content(), '<p>')
I had this exact same problem a while ago. Here's what I did (in single.php, which is where I had the problem):
if ( get_post_format() == 'gallery' ) :
$content = get_the_content();
$gallery_regex = '/\[gallery.*]/s'; //identify the [gallery] tags within the content
//get gallery code
$gallery = preg_match($gallery_regex, $content, $matches);
$gallery = $matches[0];
//remove gallery from content
add_filter('the_content', function($content){
$gallery_regex = '/\[gallery.*]\s*/s';
return preg_replace($gallery_regex, ' ', $content);
});
endif;
Basically, I used regex to remove the gallery tags from the content.
$gallery still contains the shortcode. We can't just randomly display it, or it'll actually show the shortcode. We need to execute it, which will show the output:
if ( get_post_format() == 'gallery' ) {
echo '<div id="left">'. do_shortcode($gallery) .'</div>';
}
Your content no longer contains the gallery, so you can do this:
<div id="right"><?php the_content(); ?></div>

Auto enable embed youtube video with get_post() function

I use get_post() function to get a specific post content. However, I cannot make the auto embed video runs.
Here is the code
<?php
$post_id = 110;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
I want it auto detect the youtube link and enable the embed video.
The sample content is
Check out this cool video:
http://www.youtube.com/watch?v=nTDNLUzjkpg
That was a cool video.
http://codex.wordpress.org/Function_Reference/get_post
http://codex.wordpress.org/Embeds
I haven't tried this myself, but this is what I can tell you:
The oEmbed functionality is applied in wordpress by a filter.
The get_posts() function suppresses filters by default and I guess get_post() does the same, although I was not able to verify this in the documentation.
Perhaps you can use query_posts() (which doesn't suppress filters) or get_posts() with suppress_filters=>false to test this.

Categories