How to write to variable get_template_file() ?
-
Hello everyone.
I need to make something unusual.Everyone knows funtion get_template_part();
So, what i’m trying to do is:
I have a function that accepts few arguments, then makes a custom wp_query and loops throught posts (and with changing variables must change content).
So what i’m trying to do now is make get_template_part() not to echo the content inside but return it to my variable.
Quite ununderstandible…may be code will help you to get my idea:
Somewere in file (not in loop)$contents = unit_get_posts(array('post_type'=>'services','file_url'=>'services')); echo '<pre>'; var_dump($contents); echo '</pre>';
Function in functions.php:
function unit_get_posts($args = array() ){ $defaults = array ( 'file_pre' => 'ajax', 'file_url' => 'news', 'post_type' => 'post', 'posts_per_page'=> 10, 'offset' => 0, 'echo' => false ); $args = apply_filters( 'unit_get_posts', $args ); extract( wp_parse_args($args, $defaults), EXTR_SKIP ); $return = ''; $posts = new WP_Query(array( 'post_type' => $post_type, 'posts_per_page' => $posts_per_page, 'offset' => $offset )); if($posts->have_posts()): while($posts->have_posts()): $posts->the_post(); $return .= load_template_part($file_pre, $file_url); endwhile; wp_reset_query(); else: $return = false; endif; if( $echo ){ if( empty( $return ) ){ echo 0; } else { echo $return; } } else { return $return; } die(); } function load_template_part($template_name, $part_name=null) { ob_start(); get_template_part($template_name, $part_name); $var = ob_get_contents(); ob_end_clean(); return $var; }
But how hard i try my variable $return (on line $return .= load_template_part($file_pre, $file_url); ) doesn’t gets any value..
I just don’t know how to make return of echo..
how to make something like thesefunction first(){ echo '123'; } function two(){ $text = first(); $text = str_replace('1','2',$text); return $text; } two();
If someone knows how to do that PLESE tell me!!
- The topic ‘How to write to variable get_template_file() ?’ is closed to new replies.