简体   繁体   中英

Use different page template if ative content In Sidebar

Im using the UnderScore theme and below is the main page.php template file. What im aiming for is if there is active widgets in the sidebar use template with sidebar but if no active widgets or content use template that has the main content as 960px with no sidebar. Im also using ACF (advance custom fields) is the sidebar so would need to check that aswell. Gratefull for any help. I thought maybe using "is_active_sidebar" but unsure how to implerment it properly as template parts are already being check.

get_header(); ?>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        while ( have_posts() ) : the_post();
            get_template_part( 'template-parts/content', 'page' );
            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;
        endwhile; // End of the loop.
        ?>
    </main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();

Also below the sidebar.php

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
    return;
}
?>
<aside id="secondary" class="widget-area" role="complementary">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
    <?php the_field('sidebar_info'); ?>
</aside><!-- #secondary -->

Maybe I'm not fully understanding you question, but you can use a simple if-statement for that. See pseudo code below.

if ( active widgets in the sidebar ) {
  // use this formatting
} else {
  // use that formatting
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM