Display multiple ads Between post - wordpress

For multiple ads on post
I have implemented first code and it perfectly work. But iam not good at coding so can you tell me how to show multiple ads? This second code how to implement it properly can you help me with it?
Also for multiple ads do i need to create second in article ads code? Or i can use the same ads code twice?
//Insert ads after third paragraph of post content.
add_filter( 'the_content', 'insert_post_ads' );
function insert_post_ads( $content ) {
$ad_code = '<div>Insert your Ads code here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 3, $content );
}
return $content;
}
// Parent Function
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 );
}
And this second code:
$first_ad_code = ‘Ads code goes here’;
$second_ad_code = $first_ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $first_ad_code, 3, $content );
$content = prefix_insert_after_paragraph( $second_ad_code, 6, $content );
return $content;
}
return $content;
}
I have collected this two code from this blog: https://www.codefixup.com/wordpress-insert-ads-between-posts/
i have astra child theme. these code will be pasted on child theme function.php
Please just edit these code as a one pls.

Related

i have to add prefix in some pages of my website and it show the conent of my existing page (mean without prefix page) in wordpress

$items = array( 'page' );
foreach( $items as $item )
{
add_filter( $item . '_link', 'my_link', 99, 2 );
}
function my_link($permalink, $post )
{
$pagearr = array('daily-dairy', 'free-downloads','latest-essay-topics','practice- tests','practice-exercises','video-lectures','contacts','sample1','sample3');
$arr = explode('/',$permalink);
if(in_array($arr[sizeof($arr)-2],$pagearr)){
$permalink = str_replace( get_site_url(), get_site_url() . '/institute', $permalink );
return $permalink;
}else{
return $permalink;
}
}
This code adds a prefix in my page URL but it shows page not found when add the prefix. I have to show the content without the prefix page.
you haven't mentioned here that which hook you are using for these functions, you have written in your code. Look at the code below and tell me if you find it working on your end.
function change_blog_links($post_link, $id=0){
$post = get_post($id);
if( is_object($post) && $post->post_type == 'post' || $post->post_type == 'page'){
return home_url('/YOUR_PREFIX/'. $post->post_name.'/');
}
return $post_link;
}
add_filter('post_link', 'change_blog_links', 1, 3);

Remove Yoast Breadcrumb Last Item On Single Pages Only

i got this code from researching and trying to modify it but no luck. I want the last item in yoast breamcrumbs not to appear on single pages only
if ( is_single() ) {
/*Remove Last item in Yoast SEO Breadcrumb */
function adjust_single_breadcrumb( $link_output) {
if(strpos( $link_output, 'breadcrumb_last' ) !== false ) {
$link_output = '';
}
return $link_output;
}
add_filter('wpseo_breadcrumb_single_link', 'adjust_single_breadcrumb' );
};
I tried adding if is single in between the function and also this
if(strpos( $link_output, 'breadcrumb_last' ) !== false && is_single) {
unfortunately, the whole breadcrumb disappears.
with this code, you can remove post title in single page.
add_filter('wpseo_breadcrumb_single_link_info', 'remove_post_title_wpseo_breadcrumb', 10, 3);
function remove_post_title_wpseo_breadcrumb($link_info, $index, $crumbs)
{
if (is_singular() && isset($link_info['id']))
return [];
return $link_info;
}
Updated: In the latest version above code does not work perfectly.
for this reason, I removed the last item with regex:
function getBreadcrumb() {
if ( function_exists( 'yoast_breadcrumb' ) ) {
ob_start();
yoast_breadcrumb( '<p id="breadcrumb" class="meta-info">', '</p>' );
$breadcrumb = trim( ob_get_clean() );
if ( is_singular() )
$breadcrumb = trim( preg_replace( '/ ' . WPSEO_Options::get( 'breadcrumbs-sep' ) . ' <span class="breadcrumb_last" aria-current="page">(.*)<\/span>/i', '',
$breadcrumb ) );
echo $breadcrumb;
}
}
And call the function anywhere you want:
<?php getBreadcrumb() ?>

Place ads before last paragraph in wordpress

Here is the code for place ads after 1st, 2nd, 3d paragraph. but i want to place ad just before last paragraph of my wordpress post. is there any way to do this ?
<?php
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $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 );
}
?>
add_filter('the_content', 'ad_signal');
function ad_signal($content)
{
if (!is_single()) return $content;
$tent = get_the_content();
$content = explode("</p>", $content);
$ss = count($content);
$ns = $ss-1; //**before last paragraph in wordpress **
$new_content = '';
for ($i = 0; $i < count($content); $i++) {
if ($i == $ns ) {
$new_content.= ' <div style="text-align:center;">';
$new_content.= '<h1> Your ads Here ! </h1>';
$new_content.= '</div>';
}
$new_content.= $content[$i] . "</p>";
}
return $new_content;
}
I have set Adsense code by this Code in my sites named All BD Results and etunescafe. You just add this code in your sites functions.php N.B. Before adding the code please replace your or add your adsense code. It will be good enough to go through this post. How to Set Adsense Ads Right Before The Last Paragraph
function ads_added_above_last_p($text) {
if( is_single() ) :
$ads_text = '<div class="a" style="text-align: center;">Your Adsense Code</div>';
if($pos1 = strrpos($text, '<p>')){
$text1 = substr($text, 0, $pos1);
$text2 = substr($text, $pos1);
$text = $text1 . $ads_text . $text2;
}
endif;
return $text;
}
add_filter('the_content', 'ads_added_above_last_p');
Hey guys so I ran into the same problem and managed to solve it incorporating your answers into one.
The function that does all the processing is changed so that is counts all the paragraphs created by the explode method. Then another if statement is added to evaluate when you are at the desired location.
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
$last_paragraph_index = count($paragraphs);
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
else if( $last_paragraph_index + $paragraph_id == $index + 1){
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
in order to use the new functionality you must call it with a negative number.
Think of the function this way, if you want to go from the top of the content you use a positive number, if you want to go from the bottom of the content you use a negative number.
Remember that even though the evaluation is for your decided spot such as before the last paragraph the condition that adds to the array uses the index which we all know is zero based.
This just means that to get after paragraph 1 you must use the number 2 when calling the function and to get to before the last paragraph you have to use -2 and so on.
Here is my example to add the a read the next post if it exists to the article
add_filter( 'the_content', 'prefix_insert_next_article_banner' );
function prefix_insert_next_article_banner( $content ) {
if ( is_single() && ! is_admin() && get_next_post_link() ) {
$next_banner = '<div class="next-banner"> <span> Read Next </span>';
$next_banner .= ' ' . get_the_title(get_adjacent_post(false,'',false)) .' ';
$next_banner .= '</div>';
return prefix_insert_after_paragraph( $next_banner, -2, $content );
}
return $content;
}

Highlighting page in menu on custom post types

I have a page called "Portfolio". I use this page to show the archive for my custom post type called "Works". I do this by displaying the portfolio page on with a custom template called "Work archive".
I would like to highlight the Portfolio page in my menu when I am on a single post of Works.
Can You help me?
This could help you
function change_page_menu_classes($menu){
global $post;
if (get_post_type($post) == 'portfolio')
{
$menu = str_replace( 'current_page_parent', '', $menu ); // remove all current_page_parent classes
$menu = str_replace( 'page-item-366', 'page-item-366 current_page_parent', $menu ); // add the current_page_parent class to the page you want
}
return $menu;
}
add_filter( 'wp_page_menu', 'change_page_menu_classes', 0 );
Source
Hey I dunno if this is still relevant but I came across this and it worked great. I'm using the roots theme with a post type of "projects"
// Remove active class from menu
function remove_active_class($class) {
return ( $class == 'active' ) ? FALSE : TRUE;
}
// Add active class to menu of post type single template
function add_class_to_wp_nav_menu($classes) {
if( is_singular( 'projects' ) ) {
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-projects', $classes) ) {
$classes[] = 'active';
}
} elseif( is_singular( 'resources' ) ) {
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-resources', $classes) ) {
$classes[] = 'active';
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
function namespace_menu_classes( $classes , $item ){
if ( get_post_type() == 'attorneys' ) {
$classes = str_replace( 'current_page_parent', '', $classes );
if ( $item->url == '/attorneys' ) {
// Replace "attorneys" with your code
if(preg_match('/attorneys/', $item->url)) {
$classes = str_replace( 'menu-item', 'menu-item current_page_parent', $classes );
}
}
return $classes;
}
Altered from here: https://wordpress.org/support/topic/custom-post-type-highlighting-current-menu-item

Wordpress remove shortcode and save for use elsewhere

Trying to remove the gallery shortcode from the post content and save in a variable for use elsewhere in the template. The new Wordpress gallery tool is great for selecting which images they want and assigning captions, hoping to use this to create the gallery, but then pull it out of the content on the front-end.
So this little snipped works just fine for removing the gallery and reapplying formatting... however I want to save that gallery shortcode.
$content = strip_shortcodes( get_the_content() );
$content = apply_filters('the_content', $content);
echo $content;
Hoping to save the shortcode so it can be parsed into an array and used to recreate a custom gallery setup on the front-end. An example of this shortcode I'm trying to save is...
[gallery ids="1079,1073,1074,1075,1078"]
Any suggestions would be greatly appreciated.
Function to grab First Gallery shortcode from post content:
// Return first gallery shortcode
function get_shortcode_gallery ( $post = 0 ) {
if ( $post = get_post($post) ) {
$post_gallery = get_post_gallery($post, false);
if ( ! empty($post_gallery) ) {
$shortcode = "[gallery";
foreach ( $post_gallery as $att => $val ) {
if ( $att !== 'src') {
if ( $att === 'size') $val = "full"; // Set custom attribute value
$shortcode .= " ". $att .'="'. $val .'"'; // Add attribute name and value ( attribute="value")
}
}
$shortcode .= "]";
return $shortcode;
}
}
}
// Example of how to use:
echo do_shortcode( get_shortcode_gallery() );
Function to delete First gallery shortcode from Post content:
// Deletes first gallery shortcode and returns content
function strip_shortcode_gallery( $content ) {
preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
if ( ! empty( $matches ) ) {
foreach ( $matches as $shortcode ) {
if ( 'gallery' === $shortcode[2] ) {
$pos = strpos( $content, $shortcode[0] );
if ($pos !== false)
return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
}
}
}
return $content;
}
// Example of how to use:
$content = strip_shortcode_gallery( get_the_content() ); // Delete first gallery shortcode from post content
$content = str_replace( ']]>', ']]>', apply_filters( 'the_content', $content ) ); // Apply filter to achieve the same output that the_content() returns
echo $content;
just use the get_shortcode_regex():
<?php
$pattern = get_shortcode_regex();
preg_match_all('/'.$pattern.'/s', $post->post_content, $shortcodes);
?>
that will return an array of all the shortcodes in your content, which you can then output wherever you feel, like so:
<?php
echo do_shortcode($shortcodes[0][1]);
?>
similarly, you could use the array entries to check for shortcodes in your content and remove them with str_replace():
<?php
$content = $post->post_content;
$content = str_replace($shortcodes[0][1],'',$content);
?>
Something like $gallery = do_shortcode('[gallery]'); might work.

Resources