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.
Related
I have this shortcode:
I do this shortcode in template, I tried several WordPress themes, and it does not return HTML, but just text in continuous row.
I used this shortcode way abck and it worked, I do not know what happened meanwhile in a year so it does not work in WordPress anymore.
Anyone has a clue?
function paywall_level_b( $atts, $content = null ) {
$post = get_queried_object();
if ( function_exists( 'pmpro_has_membership_access' )) {
// Check if the user has access to the post.
$hasaccess = pmpro_has_membership_access( $post->ID );
// Display Content if the user has access to the post.
if( ! empty( $hasaccess ) ) {
$content = apply_filters('the_content', get_the_content());
echo $content;
// Display Text if the user has no access to the post, but is logged in.
} elseif ( is_user_logged_in() ) {
$content = apply_filters('the_content', get_the_content());
$sentences = explode(".", $content);
$first_slice = implode(".", array_splice($sentences, 0, 5));
$second_slice = implode(".", array_splice($sentences, 0));
echo '<div class="non-paywall">'. html_entity_decode ( $first_slice ) .'.</div><div class="pmpro_content_message">Text</div>';
// Display Text if the user has no access to the post, and is not logged in.
} else {
$content = apply_filters('the_content', get_the_content());
$sentences = explode(".", $content);
$first_slice = implode(".", array_splice($sentences, 0, 5));
$second_slice = implode(".", array_splice($sentences, 0));
echo '<div class="non-paywall">'. html_entity_decode ( $first_slice ) .'.</div><div class="pmpro_content_message">Text 2</div>';
}
}
}
add_shortcode( 'paywall_level_b', 'paywall_level_b' );
I am using the code below to execute video shortcode on my WordPress website but some pages already contain manually added video which will cause duplicate when i use the code.
How can include a check if the page already contain a YouTube embedded iframe or video link and exclude pages which already have videos, here is what i have below:
if (is_single() && in_category(1) )
{
echo '<h4 class="post-title entry-title">Video</h4>' ;
echo do_shortcode( '[yotuwp type="keyword" id="'.get_post_field( 'post_title', $post_id, 'raw' ).'" player="mode=large" template="mix" column="1" per_page="1"]' );
}
I want to include youtube link check here:
if (is_single() && in_category(1)
Here is what i am able to find Here but this scans the requested url instead of the content on it:
<?php
if (stripos($_SERVER['REQUEST_URI'],'tout') == true && stripos($_SERVER['REQUEST_URI'],'dedans') == true)
{echo '<div class="clear"></div> >> View all Cakes';}
?>
Since you already have the $post_id I suggest you get the Post object and do a regular expression match for 'youtube' or the short URL version 'youtu.be'. See sample code:
$post = get_post($post_id);
$content = apply_filters('the_content', $post->post_content);
if (is_single() && in_category(1) && !preg_match('/youtu\.?be/', $content)) {
echo '<h4 class="post-title entry-title">Video</h4>';
echo do_shortcode('[yotuwp type="keyword" id="' . get_post_field('post_title', $post_id, 'raw') . '" player="mode=large" template="mix" column="1" per_page="1"]');
}
Use file_get_contents, like this:
if (strpos(file_get_contents($_SERVER['REQUEST_URI']), 'youtube') === false) {
// you can add your video
}
I would recommand you to hook on the_content filter to check the current content if already has the wanted shortcode, if not, add it.
add_filter( 'the_content', 'func_53829055', 10 );
/**
* Check if the shortcode exists, if not, add it
*
* #param string $post_content
*
* #return string
*/
function func_53829055( $post_content ) {
// Add your custom conditions
if( ! is_single() && ! in_category( 1 ) ) {
return $post_content;
}
// Use a regex or strpos depending on the needs and post_content length/complexity
if ( false !== strpos( $post_content, '[yotuwp' ) ) {
// It already has the wanted shortcode, then return the post content
return $post_content;
}
// If add the video before post content
$before = '<h4 class="post-title entry-title">Video</h4>';
$before .= do_shortcode( 'my_shortcode' );
$before .= '<br />';
$post_content = $before . $post_content;
// If add the video after post content
$post_content .= '<br />';
$post_content .= '<h4 class="post-title entry-title">Video</h4>';
$post_content .= do_shortcode( 'my_shortcode' );
return $post_content;
}
Note that the priority has to be lower than 11, because shortcodes are replaced on 11 : add_filter( 'the_content', 'do_shortcode', 11 );
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' );
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 want to display full content instead the post excerpt. I didn't want to modify the theme. I just want to make a plugin where I will put the file.
I found something like this
function narga_excerpts($content = false) {
# If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;
$content = strip_shortcodes($content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
# If an excerpt is set in the Optional Excerpt box
if($content) :
$content = apply_filters('the_excerpt', $content);
# If no excerpt is set
else :
$content = $post->post_content;
$excerpt_length = 50;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...<p><a class="more-link" href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '"> ' . __( 'Read more ยป', 'narga' ) . ' </a></p>');
$content = implode(' ', $words);
endif;
$content = '<p>' . $content . '</p>';
endif;
endif;
return $content;
}
add_filter('the_content', 'narga_excerpts')
But it's totally different. The code modify the_content to show the_excerpts. How can I change the code to display WordPress full content instead post excerpt.
Using the_content() will display the full content of a post.
Changing the function to this should work
function narga_excerpts($content = false)
{
# If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_content;
endif;
return $content;
}
add_filter('the_content', 'narga_excerpts')