WordPress + PHP - How can I find out whether there are less paragraphs in a record? -
i have (tortured) script:
/*----------------------------- 3 ---------------------------*/ $paragraphafter3 = 3; *------------------------ widget 3 ----------------------------*/ //widget area add_action( 'widgets_init', 'register_my_widgets_google-ads-3' ); function register_my_widgets_main_google_ads_3(){ register_sidebar( array( /* 'name' => sprintf(__('sidebar %d'), $i ), */ 'name' => 'my code 3', /* 'id' => "sidebar-$i", */ 'id' => 'google-ads-3', 'description' => '', 'class' => '', 'before_widget' => '<div id="%1$s" class="widget %2$s">', 'after_widget' => "</div>\n", 'before_title' => '<h2 class="google-ads-3 widgettitle">', 'after_title' => "</h2>\n", ) ); } add_action( 'widgets_init', 'register_my_widgets_main_google_ads_3' ); /* function googleads1(){ if ( function_exists('register_my_widgets_google-ads-1') ) dynamic_sidebar(google-ads-1); }; */ add_filter( 'the_content', 'wpse_content_google_ads_3' ); function wpse_content_google_ads_3( $content ) { if( !is_single() ) return $content; /*number paragraf*/ /* $paragraphafter = 3; //number of paragraph */ global $paragraphafter3; $paragraphafter = $paragraphafter3; //number of paragraph $content = explode ( "</p>", $content ); $new_content = ''; ( $a = 0; $a < count ( $content ); $a ++ ) { if ( $a == $paragraphafter ) { ob_start(); if ( is_active_sidebar( 'google-ads-3' ) ) : dynamic_sidebar( 'google-ads-3' ); else : echo '<span></span>'; endif; $result = ob_get_clean(); $new_content .= $result ; } elseif ($a != $paragraphafter) { $new_content .= ''; } $new_content .= $content[$a] . "</p>"; } return $new_content; }
this script inserts contents of widget through number of paragraphs, , works well. if there fewer paragraphs on page. glues contents of widgets footer. how achieve not display widget if there fewer paragraphs in post indicated in it?
since exploding contents using $content = explode ( "</p>", $content );
, last part of loop not going have
tag, might loop until 2nd last value this:
for ( $a = 0; $a < count ( $content )-1; $a ++ ) //**notice - 1 here** { ... ... }
but huncha code messing dom , thats why results of widgets moving footer. once case see happening if $paragraphafter = 0
. in case result of widget added before first part of content
might mess dom , make widget show in footer.
Comments
Post a Comment