I want to make a shortcode and in shortcode i want to require a file. when a user write a shortcode in editor. The output will show the required file layout.
I make a shortcode but this not working, here is my 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 is my 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 the 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 as $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 all 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 only widgets and all layout get messy.
any idea
You have two issues here.
First,
require
doesn’t return anything. It just “imports” the code from the file and runs it exactly where you put the require call. So, everything in your new file is displayed before the rest of the stuff WordPress handles.You can circumvent this with an output buffer. This:
Should look like this:
Now you can return the string and WordPress will do the rest.
Second problem: your
get_header()
andget_footer()
calls. WordPress will already have printed it’s header, your call will display it a second time. Same goes for the footer … your included file will display the footer and WordPress will display it a second time at the end of the page. You should remove both calls from your included file.Using the output buffer for this is not a very clean solution. It would be better to place all your output in the included file in a function that stores everything in a variable that is returned by the function. Then you could do: