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 'taxonomy-foo_cat.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
    get_header();
    $foo_cat_slug = get_queried_object()->slug;
    $foo_cat_name = get_queried_object()->name;
    $foo_tax_post_args = array(
        'post_type' => FOO_POST_TYPE,
        'posts_per_page' => 999,
        'orderby' => 'name',
        'order' => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => FOO_POST_TAXONOMY,
                'field' => 'slug',
                'terms' => $foo_cat_slug
            )
        )
    );
    $foo_tax_post_qry = new WP_Query($foo_tax_post_args);
?>
<div id="foo_container">
    <!--Breadcrum-->
    <?php
        if(FOO_BREADCRUMBS_SETTING == 1){
    ?>
        <div class="foo_breadcrum">
            <?php 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">
            <!--<articles>-->
            <div class="foo_articles">
                <h2><strong><?php echo $foo_cat_name; ?></strong></h2>
                <ul>
            <?php
                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;
                endif;
            ?>
                </ul>
            </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

Read more here: How to include a file in shortcode function in wordpress


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: