Get first paragraph in wordpress - php

I have a problem with getting the first form the_excerpt();
Function acutally works but only for first post. I added in functions.php
function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
$str = strip_tags($str, '<a><strong><em>');
return '<p>' . $str . '</p>';
}
I'm calling this funcion in index.php inside The Loop <?php echo get_first_paragraph(); ?>
I have no idea why it pulls only for first post...

you can put this code in function.php file in your theme,
Get first paragraph
function awesome_excerpt($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', 'awesome_excerpt', 10, 2 );
For more information, you can follow the reference link WORDPRESS THE_EXCERPT SHOW ONLY FIRST PARAGRAPH

That code didn't work for me in Gutenberg times. So I've used part of that code and did some searching and come up with this solution. Hope it helps.
function get_paragraph_content($paragraph_number){
global $post;
$i = 0;
$paragraph = '';
if ( has_blocks( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
foreach( $blocks as $block ) {
if( 'core/paragraph' === $block['blockName']){
$paragraph = render_block($block);
if (++$i == $paragraph_number) break;
}
}
$paragraph = substr( $paragraph, 0, strpos( $paragraph, '</p>' ) + 4 );
$paragraph = strip_tags($paragraph, '<a><strong><em>');
}
return $paragraph;
}

Related

No ads are loading on amp-mode-touch

Like the title says. I’ve successfully placed adsense ads on my AMP posts. While testing it out on my desktop via firebug, adverts are coming up as intended. However, I cannot get any ads to come up on my mobile? The pages body class changes to amp-mode-touch and the adverts div containers disappear completely.
Here is an example URL - http://www.gamersgreed.com/kingdom-come-deliverance-fps-boost-optimization/amp
And here is the code i'm using to produce the ads -
//Insert ads after third and seventh paragraph of any single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div style="text-align: center;"><amp-ad width=300 height=250
type="adsense"
data-ad-client="ca-pub-7568065840567088"
data-ad-slot="1234194857">
<div placeholder></div>
<div fallback></div>
</amp-ad>
</div>
';
$ad_code2 = $ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $ad_code, 2, $content );
$content = prefix_insert_after_paragraph( $ad_code2, 7, $content );
return $content;
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}

Display 1st paragraph within Advanced Custom Fields

How do you display only the first paragraph of an advanced custom field.
<?php the_field('lyrics'); ?>
Above is what i use to display the full text.
add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
function my_custom_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
try this code will help you to show first 55 character of your first paragraph.
Grab the first paragraph of each post
second option:
function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
$start = strpos($text, '<p>'); // Locate the first paragraph tag
$end = strpos($text, '</p>', $start); // Locate the first paragraph closing tag
$text = substr($text, $start, $end-$start+4); // Trim off everything after the closing paragraph tag
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
return $text;}
third option :
You can use this function:
function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
$str = strip_tags($str, '<a><strong><em>');
return '<p>' . $str . '</p>';}
and then use in it in your loop with:
<?php echo get_first_paragraph(); ?>

WordPress excerpt to show 2 paragraphs

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.

Echo a variable inside a variable

I'm trying to figure out how to echo a variable inside a variable.
This code below doesn't obviously work because I'm not echoing the variable
$tweet = get_field('tweet_msg'); //this is getting the string inputted by user in the custom field
$tweet_intent = '<div>TEST </div>';
but when ever I do PHP throws an error saying unexpected echo:
$tweet_intent = '<div style="margin-bottom:15px;">TEST </div>';
Full code:
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$tweet = get_field('tweet_msg');
$tweet_intent = '<div style="margin-bottom:15px;">TEST </div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $tweet_intent, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
} return implode( '', $paragraphs );
}
The problem reside in your <a href> syntax.
Assuming that, after get_field(), the value of $tweet is 'Hello-World', your code:
$tweet_intent = '<div style="margin-bottom:15px;">TEST </div>';
put in $tweet_intent this string:
(...)TEST </div>
└──────────────────────────────────────┘
As you can see, the quotation marks of href are closed before $tweet output.
You have to change your code in this way:
$tweet = get_field( 'tweet_msg' );
$tweet = rawurlencode( $tweet ); // only if encoding is not performed by get_field
$tweet_intent = '
<div style="margin-bottom:15px;">
TEST
</div>';

Adding Ads to Second Paragraph of WordPress Page

I have the following code that I'm using in my functions.php file to add a Google ad to the second paragraph of each post. As it stands, the code works but only on posts. I was hoping someone on this forum could assist with tweaking the code so this function is applied to pages as well to posts.
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>AD Code to go here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
Instead of is_single(), which only tests whether a single posts are shown, you should use is_singular(), which test for is_single(), is_page() or is_attachment()

Categories