I want to add Advertisement after end 7 th paragraph ,after end 22 paragraph and after end 30 th paragraph on my the_content(). How can i do it ?
I try to used below code but there have foreach section because of that i cant used another loop under foreach
<?php
$content = apply_filters( 'the_content', $post->post_content );
if( substr_count( $content, '<p>' ) > $insert_after )
{
$closing_p_tag = '</p>';
$contents = explode( '</p>', $content );
$p_tag_counter = 1;
foreach( $contents as $content )
{
echo $content;
echo $closing_p_tag;
if( $p_tag_counter == 7)
{
?>
<?
}
echo $closing_p_tag;
if( $p_tag_counter == 22)
{
?>
<?
}
echo $closing_p_tag;
if( $p_tag_counter == 30)
{
?>
<?
}
echo $closing_p_tag;
$p_tag_counter++;
}
}
?>
Related
The closing P tag </p> is stripped out of my blog posts whenever I use this code in my Functions.php file to insert an after a number of paragraphs. It leaves the <p> unclosed after every paragraph. What could be wrong?
// Article Ads After X Paragraphs
/////////////////////////////////////
$mvp_post_inad = get_option('mvp_post_inad');
if (!empty($mvp_post_inad)) {
function mvp_post_ad_insert( $text ) {
if ( is_single() ) {
$mvp_post_inad = get_option('mvp_post_inad');
if ($mvp_post_inad) {
$ads_text = '<div class="mvp-post-ad-wrap"><span class="mvp-ad-label">' . esc_html__( 'Advertisement', 'zox-news' ) . '</span><div class="mvp-post-ad">'.do_shortcode(html_entity_decode($mvp_post_inad)).'</div></div>';
}
$split_by = "</p>";
$mvp_post_freq = get_option('mvp_post_freq');
$insert_after = $mvp_post_freq; //number of paragraphs
// make array of paragraphs
$paragraphs = explode( $split_by, wptexturize($text));
if ( count( $paragraphs ) > $insert_after ) {
$new_text = '';
$i = 1;
foreach( $paragraphs as $paragraph ) {
if( preg_match( '~<(?:img|blockquote|ul|li)[ >]~', $paragraph )) {
$new_text .= $paragraph;
} else {
$new_text .= $paragraph . ( $i % $insert_after == 0 ? $ads_text : '' );
$i++;
}
}
return $new_text;
}
}
return $text;
}
add_filter('the_content', 'mvp_post_ad_insert');
}
I want to add new conditions to the following code:
function add_thumb_after_h2( $content ) {
if (is_single()) {
global $post;
if ( substr_count( $content, '<h2>' ) > 3 ) {
$thumb = '<div id="post_thumbnail">' . get_the_post_thumbnail( $post->ID, 'fullsize' ) . '</div>';
$content = str_replace_once("</h2>", "</h2>".$thumb, $content);
}
}
return $content;
}
I also need to include h3, h4 and h5, example:
if ( substr_count( $content, '<h2>,<h3>,<h4>,<h5>' ) > 3 ) {}
How can I do it?
The thumbnail should be added only to the first occurrence of a heading tag, regardless of its type (h2, h3, etc.)
Thanks
You can achieve this my modifying your function to the following:
function add_thumb_after_h2( $content ) {
if ( is_single() ) {
global $post;
foreach ( array('h2', 'h3', 'h4', 'h5') as $heading ) {
if ( ! $replaced && substr_count( $content, "<{$heading}>" ) > 3 ) {
$thumb = '<div id="post_thumbnail">' . get_the_post_thumbnail( $post->ID, 'fullsize' ) . '</div>';
$content = str_replace_once("</{$heading}>", "</{$heading}>" . $thumb, $content);
break;
}
}
}
return $content;
}
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;
}
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'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>';