So I got this code, which I am trying to adapt to my needs. It should and does wrap a div around the necessary <img> tags. But when I add the "if (is_page...)" all other sites have no content anymore. I am very confused as to why this is happening since the code should not execute or do anything when the if statement is not met.
function breezer_addDivToImage( $content ) {
if ( is_page_template('single.php') ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$replacement = '<div class="myphoto">$1</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
}
add_filter( 'the_content', 'breezer_addDivToImage' );
/* Place custom code above this line. */
?>
This is how i display the content on the page
<!--S3 show if dynamic page (blogposts)-->
<?php } else if (is_page() == false) {
if(have_posts()) :
while(have_posts()) : the_post();
if ( has_post_thumbnail() ) {
<img class="responsive-img" src="<?php the_post_thumbnail_url(); ?>">
<?php } ?>
<h2><?php the_title(); ?></h2>
<?php the_content();
endwhile;
else : ?>
<h3></h3>
<p></p>
<?php endif;
} ?>
<!--S3 end "show if dynamic page"-->
you filter your content of all pages and posts with this hook and only return the content if the condition is true.
Put simply a else {return $content} to your breezer_addDivToImage function.
function breezer_addDivToImage( $content ) {
if ( is_page_template('single.php') ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$replacement = '<div class="myphoto">$1</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
else {
return $content;
}
}
add_filter( 'the_content', 'breezer_addDivToImage' );
Related
I have a custom meta field that I would like to insert in the_content automatically so that my AMP plugin can render the custom field value in the same way as the_content.
Currently I am using this code to display it:
<?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?>
<?php if ( ! empty( $video_value ) ) {?>
<div class="video-container"><?php echo $video_value; ?></div>
<?php } else { ?>
<?php the_post_thumbnail(); ?>
<?php } ?>
But I would like the $video_value to be inserted automatically before the_content.
You can use the the_content filter to do this. You can read more about it on the WordPress developer site.
But the code could be looking something like this:
function my_custom_content_filter($content){
global $post;
$video_value = get_post_meta($post->ID, '_video', true);
if($video_value){
return '<div class="video-container">' . $video_value . '</div>' . $content;
}else{
return get_the_post_thumbnail($post->ID) . $content;
}
}
add_filter('the_content', 'my_custom_content_filter');
And you can add this code in you functions.php file.
Note This filter only works on the_content() and not get_the_content()
You can do something like this -
and add the conditions that you want -
You might need to access Global $post to get the meta value
function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true) {
$beforecontent = 'This line will go before the content - populate with whatever.';
$aftercontent = 'This will come after the content - makes sense right?';
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');
You can add this in functions.php
I have created two categories within a WordPress site with one post in each category. I am pulling in and excerpt from each category post on different pages.
I am showing the post excerpt like so on each page and adding a read more link manually.
<?php query_posts('cat=4&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
Read More
<?php endwhile; ?>
The following code will then end the excerpt after the first paragraph.
function post_single_paragrapgh($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
add_filter( 'wp_trim_excerpt', 'post_single_paragrapgh', 10, 2 );
What I would like to do is tell it to cut off after a second paragraph or, in fact an image and a paragraph.
It will pull in an image if there is one at the top of post but then I also want a further paragraph after the image or just two paragraphs of text. Either or.
I used this site for reference but method C1 throws up errors.
https://www.bybe.net/wordpress-the_excerpt-show-first-paragraph/
Thanks in advance!
Are you open to a solution with regex? Look for <p>(Anything)</p> and merge the two first occurrence. The code is not tested but should work.
function post_single_paragrapgh($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
preg_match_all( '~<p>(.*?)</p>~', $content, $matches );
if( isset( $matches[0][0] ) && isset( $matches[0][1] ) ) {
$text = $matches[0][0] . $matches[0][1];
} else {
$text = $matches[0][0];
}
}
return $text;
}
I managed to figure out what I needed with this code, posted the question too hastily I guess.
function awesome_excerpt($awesomeness_excerpt) {
global $post;
$raw_excerpt = $awesomeness_excerpt;
if ( '' == $awesomeness_excerpt ) {
$awesomeness_excerpt = get_the_content('');
$awesomeness_excerpt = strip_shortcodes( $awesomeness_excerpt );
$awesomeness_excerpt = apply_filters('the_content', $awesomeness_excerpt);
$awesomeness_excerpt = "<p>$awesomeness_excerpt</p>";
$wanted_number_of_paragraph = 2;
$tmp = explode ('</p>', $awesomeness_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$awesomeness_excerpt .= $excerpt_end;
return $awesomeness_excerpt;
}
return apply_filters('awesome_excerpt', $awesomeness_excerpt, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'awesome_excerpt');
Just took out the end if at the bottom - must have been left there in error. Hopefully it can help others.
I am working with a theme that uses the following function to set the post excerpt length on all pages:
//excerpt length
if ( !function_exists('custom_excerpt_length')):
function custom_excerpt_length( $length ) {
return 90;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function new_excerpt_more( $more ) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
endif;
I added new function:
add_filter( 'get_the_excerpt', 'so20668477_get_the_excerpt', 10, 1 );
function so20668477_get_the_excerpt( $excerpt )
{
if( is_page( 39370 ) )
$excerpt = apply_filters( 'the_content', get_the_content() );
return $excerpt;
}
But, it is not working, still showing post excerpt on page 39370. I am needing to display full length posts on Page ID = 39370 only. Not sure how to add the condition. Any help is much appreciated.
First you should be strongly encouraged to always use curly braces even in situations where they are technically optional.
Don't do:
if ( condition )
return false;
Instead do:
if ( condition ) {
return false;
}
About your question :
This function apply_filters( 'the_content', get_the_content() ); get the echo of the content not the content, so your condition is correct.
Try this code :
if( is_page( 39370 ) ){
$content = get_the_content();
$content = apply_filters('the_content', $content);
$excerpt = explode("[newpage]", $content);
}
return $excerpt;
Or there is another aproach :
if( is_page( 39370 ) ){
ob_start();
the_content();
$content = ob_get_clean();
}
return $excerpt;
I hope this solution will help you.
With this WordPress related PHP-OOP Answer, I found my solution for what I's seeking. But with the same function my_excerpt(); I want to pass another anchor tag (<a>) inside the <p> tag.
Right now, calling my_excerpt() is embracing the db texts with a paragraph tag (<p>here comes the excerpt</p>). And if I add my anchor tag like the following:
// Echoes out the excerpt
public static function output() {
the_excerpt(); ?>
<a class="read-more" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php _e( 'Read More ยป', 'my-theme'); ?></a>
<?php
}
it's showing the "Read More" just the bottom of the except texts. Inspect Element shows like the following:
<p>here comes the excerpt.</p>
Read More
How can I make changes to the function or class so that I can have the "Read More" link inside the paragraph, like:
<p>here comes the excerpt.Read More</p>
Additionally
Additionally I'm also trying to remove the [...] part from the excerpt generating by the my_excerpt();. So I tried the following:
function change_excerpt($content) {
$content = str_replace( '[...]','>',$content ); // remove [...], replace with ...
$content = strip_tags( $content ); // remove HTML
return $content;
}
add_filter('my_excerpt','change_excerpt');
I doesn't do anything to the view. But if I change it to:
add_filter('the_excerpt','change_excerpt');
Then unaware, I get the previously desired anchor tag [inside paragraph], because the filter removed the paragraph tag completely.
here comes the excerpt.Read More
But it doesn't do anything with [...] portion. :(
So, my furnished Question is:
How can I put the anchor tag inside the paragraph tag, or remove the paragraph tag, and remove the [...] part from the my_excerpt() function?
try this snippet
Include in your functions.php
function kc_excerpt( $length_callback = '', $more_callback = '' ) {
if ( function_exists( $length_callback ) )
add_filter( 'excerpt_length', $length_callback );
if ( function_exists( $more_callback ) )
add_filter( 'excerpt_more', $more_callback );
$output = get_the_excerpt();
$output = apply_filters( 'wptexturize', $output );
$output = apply_filters( 'convert_chars', $output );
$output = '<p>' . $output . '</p>';
echo $output;
}
function kc_excerpt_more( $more ) {
return '<a class="read-more" href="'. get_permalink() .'" title="'. get_the_title() .'" rel="bookmark">Read More</a>';
}
function kc_excerpt_more_2($more) {
return '...';
}
function kc_exdefault($length) {
return 10;
}
function kc_ex100($length) {
return 100;
}
And call this function from your template file
<?php kc_excerpt('kc_exdefault', 'kc_excerpt_more'); ?>
or
<?php kc_excerpt('kc_ex100', 'kc_excerpt_more_2'); ?>
I want to extract images from the_content() and want to display first images from post on index.php and want to display all images on single.php. To do this I apply a filter on the_content():
<?php
/*****************************************************************************
Post Summary Filter - Grap Image Thumbnails, Strip Tags, etc
******************************************************************************/
add_filter('the_content', 'filterContentSummary', 0);
function filterContentSummary($content){
global $post;
if( is_home() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_single ){
//if NOT single page or "page" page
$img=''; //default img code
$final_width = 300;
$content = $post->post_content;
//search for first image in article
preg_match('/<img[^>]*>/i',$content,$matches);
//image found, process it
if($matches){
preg_match('/src="[^"]*"/i',$matches[0],$src);
preg_match('/width="(\d*)[^"]*"/i',$matches[0],$width);
if($width[1] < $final_width){
$final_width = $width[1];
}
}
//prepare text
if(!empty($post->post_excerpt)){
$content = $post->post_excerpt;
}else{
//strip shortcodes, tags
$content = preg_replace('/\[\/*[^\]]*\]/i', '', $content); //cont is ready
$content = preg_replace('/<[^>]*>/i', '', $content); //cont is ready
$content = substr($content,0,500);
$content = explode(". ", $content);
array_pop($content);
$content = implode(". ", $content) . "";
}
if($content=="."){
$content = $post->post_content;
}
//prepare final content
$content = "<p>". $content ."</p>";
//Adding Read more link
$content .= "<p align='right' class='seemore'></p>";
}
// Make sure to return the content
return $content;
}
?>
On index.php using regular expressions I find the first image from the post and display it:
<?php while (have_posts()) : the_post();
$content = $post->post_content;
preg_match_all('/(<img.*src="(.*)"[^>]*>)/iU',$content,$matches);
?>
Now $matches have all the images:
<div class="content-img"><img src="<?php echo $matches[2][0];?>"/></div>
...used to display first image on a post.
But how I can display all images on single.php ?
after global $post just add if(is_single()) return $content; this way if it is single template you filter will return the original content with all the images... then display it normally.