php - How to include a file in shortcode function in wordpress -
i want make shortcode , in shortcode want require file. when user write shortcode in editor. output show required file layout.
i make shortcode not working, here shortdoe code:
<?php function inner_page( $atts, $content = null ){ $return_string = require 'foo/foo_artilces_list.php'; return $return_string; } add_action('init', 'register_section_shortcodes'); function register_section_shortcodes(){ add_shortcode('inner_page', 'inner_page'); } ?>
here require file code
<?php /*========= template name: kbe =========*/ get_header(); ?> <div id="foo_container"> <h1><?php the_title(); ?></h1> <!--breadcrum--> <?php if(foo_breadcrumbs_setting == 1){ ?> <div class="foo_breadcrum"> <?php echo foo_breadcrumbs(); ?> </div> <?php } ?> <!--/breadcrum--> <!--search field--> <?php if(foo_search_setting == 1){ ?> <div class="foo_search_field"> <input name="" type="text" placeholder="search knowledgebase..." /> </div> <?php } ?> <!--/search field--> <!--content--> <?php if(foo_sidebar_setting == 0){ ?> <div id="foo_content" class="foo_content_full" > <?php } elseif(foo_sidebar_setting == 1){ ?> <div id="foo_content" class="foo_content_right" > <?php } elseif(foo_sidebar_setting == 2){ ?> <div id="foo_content"> <?php } ?> <!--leftcol--> <div class="foo_leftcol"> <div class="foo_categories"> <?php $foo_cat_args = array( 'orderby' => 'term_order', 'order' => 'asc', 'hide_empty' => true, ); $foo_terms = get_terms(foo_post_taxonomy, $foo_cat_args); foreach($foo_terms $foo_cat){ $foo_term_id = $foo_cat->term_id; $foo_term_slug = $foo_cat->slug; $foo_term_name = $foo_cat->name; ?> <div class="foo_category"> <h2> <span class="foo_count"><?php echo foo_article_qty; ?> articles</span> <a href="<?php echo get_term_link($foo_term_slug, 'foo_cat') ?>" title="<?php sprintf( __( "view posts in %s" ), $foo_term_name ) ?>"><?php echo $foo_term_name; ?></a> </h2> <ul class="foo_article_list"> <?php $foo_tax_post_args = array( 'post_type' => foo_post_type, 'posts_per_page' => foo_article_qty, 'orderby' => 'name', 'order' => 'asc', 'tax_query' => array( array( 'taxonomy' => foo_post_taxonomy, 'field' => 'slug', 'terms' => $foo_term_slug ) ) ); $foo_tax_post_qry = new wp_query($foo_tax_post_args); if($foo_tax_post_qry->have_posts()) : while($foo_tax_post_qry->have_posts()) : $foo_tax_post_qry->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endwhile; else : echo "no posts"; endif; ?> </ul> </div> <?php } ?> </div> </div> <!--/leftcol--> <!--aside--> <?php if((foo_sidebar_setting == 2) || (foo_sidebar_setting == 1)){ dynamic_sidebar('foo_cat_widget'); } ?> <!--/aside--> </div><!--content--> </div> <?php get_footer(); ?>
it show me widgets , layout messy. idea
you have 2 issues here.
first, require
doesn't return anything. "imports" code file , runs put require call. so, in new file displayed before rest of stuff wordpress handles.
you can circumvent output buffer. this:
$return_string = require 'foo/foo_artilces_list.php';
should this:
ob_start(); require 'foo/foo_artilces_list.php'; $return_string = ob_get_flush();
now can return string , wordpress rest.
second problem: get_header()
, get_footer()
calls. wordpress have printed it's header, call display second time. same goes footer ... included file display footer , wordpress display second time @ end of page. should remove both calls included file.
using output buffer not clean solution. better place output in included file in function stores in variable returned function. do:
require('foo/foo_artilces_list.php'); $return_string = my_function();
Comments
Post a Comment