最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Force Sidebar on Full Width page

programmeradmin1浏览0评论

Any WP theme editor php gurus out there? So I'm using a theme that allows me to force all pages to full width. I have over 10,000 pages. It also forces my posts to full width too. I'm pretty sure I can edit single.php and force the right sidebar to show up but i'm having difficulty doing so. Any help is greatly appreciated. Here's the contents of my single.php file.

The end result is to have only my posts have right sidebar and not my pages that are already full width.

Thanks in advance!

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package zerif
 */

    get_header();

    $sidebar_layout = apply_filters( 'zerif_sidebar_layout', get_theme_mod( 'zerif_blog_sidebar_layout', 'sidebar-right' ) );
    $wrapper_class  = 'content-left-wrap col-md-9';

if ( 'full-width' === $sidebar_layout ) {
    $wrapper_class = 'content-left-wrap col-md-12';
}
?>
    <div class="clear"></div>
</header> <!-- / END HOME SECTION  -->
<?php
zerif_after_header_trigger();

if ( apply_filters( 'zerif_display_area_filter', true, 'single' ) ) {
    $zerif_change_to_full_width = get_theme_mod( 'zerif_change_to_full_width' );
    ?>
    <div id="content" class="site-content">
        <div class="container">
            <?php
            zerif_before_single_post_trigger();

            if ( $sidebar_layout === 'sidebar-right' && empty( $zerif_change_to_full_width ) || is_singular('post')) {
    zerif_sidebar_trigger();
}
            if(is_singular('post')){
    echo '<div class="content-left-wrap col-md-9">';
}elseif ( ! empty( $zerif_change_to_full_width ) || 'full-width' === $wrapper_class ) {
    echo '<div class="content-left-wrap col-md-12">';
} else {
    echo '<div class="content-left-wrap col-md-9">';
}

            ?>
            <?php zerif_top_single_post_trigger(); ?>
            <div id="primary" class="content-area">
                <main itemscope itemtype="" itemprop="mainContentOfPage" id="main" class="site-main">
                    <?php
                    while ( have_posts() ) {
                        the_post();
                        get_template_part( 'content', 'single' );
                        zerif_post_nav();
                        /* If comments are open or we have at least one comment, load up the comment template */
                        if ( comments_open() || '0' != get_comments_number() ) {
                            comments_template( '' );
                        }
                    }
                    ?>
                </main><!-- #main -->
            </div><!-- #primary -->
            <?php zerif_bottom_single_post_trigger(); ?>
        </div><!-- .content-left-wrap -->
        <?php
        zerif_after_single_post_trigger();

        if ( $sidebar_layout === 'sidebar-right' && empty( $zerif_change_to_full_width ) ) {
            zerif_sidebar_trigger();
        }
        ?>
    </div><!-- .container -->
    </div>
    <?php
}
get_footer(); ?>

Any WP theme editor php gurus out there? So I'm using a theme that allows me to force all pages to full width. I have over 10,000 pages. It also forces my posts to full width too. I'm pretty sure I can edit single.php and force the right sidebar to show up but i'm having difficulty doing so. Any help is greatly appreciated. Here's the contents of my single.php file.

The end result is to have only my posts have right sidebar and not my pages that are already full width.

Thanks in advance!

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package zerif
 */

    get_header();

    $sidebar_layout = apply_filters( 'zerif_sidebar_layout', get_theme_mod( 'zerif_blog_sidebar_layout', 'sidebar-right' ) );
    $wrapper_class  = 'content-left-wrap col-md-9';

if ( 'full-width' === $sidebar_layout ) {
    $wrapper_class = 'content-left-wrap col-md-12';
}
?>
    <div class="clear"></div>
</header> <!-- / END HOME SECTION  -->
<?php
zerif_after_header_trigger();

if ( apply_filters( 'zerif_display_area_filter', true, 'single' ) ) {
    $zerif_change_to_full_width = get_theme_mod( 'zerif_change_to_full_width' );
    ?>
    <div id="content" class="site-content">
        <div class="container">
            <?php
            zerif_before_single_post_trigger();

            if ( $sidebar_layout === 'sidebar-right' && empty( $zerif_change_to_full_width ) || is_singular('post')) {
    zerif_sidebar_trigger();
}
            if(is_singular('post')){
    echo '<div class="content-left-wrap col-md-9">';
}elseif ( ! empty( $zerif_change_to_full_width ) || 'full-width' === $wrapper_class ) {
    echo '<div class="content-left-wrap col-md-12">';
} else {
    echo '<div class="content-left-wrap col-md-9">';
}

            ?>
            <?php zerif_top_single_post_trigger(); ?>
            <div id="primary" class="content-area">
                <main itemscope itemtype="http://schema/WebPageElement" itemprop="mainContentOfPage" id="main" class="site-main">
                    <?php
                    while ( have_posts() ) {
                        the_post();
                        get_template_part( 'content', 'single' );
                        zerif_post_nav();
                        /* If comments are open or we have at least one comment, load up the comment template */
                        if ( comments_open() || '0' != get_comments_number() ) {
                            comments_template( '' );
                        }
                    }
                    ?>
                </main><!-- #main -->
            </div><!-- #primary -->
            <?php zerif_bottom_single_post_trigger(); ?>
        </div><!-- .content-left-wrap -->
        <?php
        zerif_after_single_post_trigger();

        if ( $sidebar_layout === 'sidebar-right' && empty( $zerif_change_to_full_width ) ) {
            zerif_sidebar_trigger();
        }
        ?>
    </div><!-- .container -->
    </div>
    <?php
}
get_footer(); ?>
Share Improve this question edited Mar 10, 2020 at 2:07 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Mar 9, 2020 at 20:27 michael2000michael2000 712 bronze badges 3
  • I'm thinking you might be able to add ` || is_singular('post')` to the 2 conditionals that start with if($sidebar_layout ===... – RiddleMeThis Commented Mar 9, 2020 at 21:06
  • Forgive my lack of php skills but where would I put that? I'm not a developer. Thank you though! – michael2000 Commented Mar 9, 2020 at 21:34
  • Meaning that below the header is the sidebar and then below the sidebar is the full width post. No sidebar on the side of the post. Just above the post. – michael2000 Commented Mar 9, 2020 at 21:54
Add a comment  | 

1 Answer 1

Reset to default 0

I'm not certain this would work without seeing the theme but you could try something like this...

Change...

if ( $sidebar_layout === 'sidebar-left' && empty( $zerif_change_to_full_width ) ) {
    zerif_sidebar_trigger();
}

To this...

if ( $sidebar_layout === 'sidebar-left' && empty( $zerif_change_to_full_width ) || is_singular('post')) {
    zerif_sidebar_trigger();
}

This, in theory, would force the left sidebar on posts because I added || is_singular('post'), which simply means OR is a post.

You could do the same thing for where its checking for the right sidebar.

if ( $sidebar_layout === 'sidebar-right' && empty( $zerif_change_to_full_width ) || is_singular('post')) {
     zerif_sidebar_trigger();
}

Since we changed our conditions around we also need to add in some logic to handle the class that is being used.

Change...

if ( ! empty( $zerif_change_to_full_width ) || 'full-width' === $wrapper_class ) {
    echo '<div class="content-left-wrap col-md-12">';
} else {
    echo '<div class="content-left-wrap col-md-9">';
}

To...

if(is_singular('post')){
    echo '<div class="content-left-wrap col-md-9">';
}elseif ( ! empty( $zerif_change_to_full_width ) || 'full-width' === $wrapper_class ) {
    echo '<div class="content-left-wrap col-md-12">';
} else {
    echo '<div class="content-left-wrap col-md-9">';
}

Update: Here is what your full code would look like with the right sidebar. Noticed I moved || is_singular('post') down to the last if statement.

<?php

/**
 * The Template for displaying all single posts.
 *
 * @package zerif
 */

get_header();

$sidebar_layout = apply_filters('zerif_sidebar_layout', get_theme_mod('zerif_blog_sidebar_layout', 'sidebar-right'));
$wrapper_class  = 'content-left-wrap col-md-9';

if ('full-width' === $sidebar_layout) {
    $wrapper_class = 'content-left-wrap col-md-12';
}

?>
<div class="clear"></div>

</header> <!-- / END HOME SECTION  -->

<?php

zerif_after_header_trigger();

if (apply_filters('zerif_display_area_filter', true, 'single')) {
    $zerif_change_to_full_width = get_theme_mod('zerif_change_to_full_width');
?>

<div id="content" class="site-content">
        <div class="container">
            <?php
    zerif_before_single_post_trigger();

    if ($sidebar_layout === 'sidebar-right' && empty($zerif_change_to_full_width)) {
        zerif_sidebar_trigger();
    }
    if (is_singular('post')) {
        echo '<div class="content-left-wrap col-md-9">';
    } elseif (!empty($zerif_change_to_full_width) || 'full-width' === $wrapper_class) {
        echo '<div class="content-left-wrap col-md-12">';
    } else {
        echo '<div class="content-left-wrap col-md-9">';
    }

    ?>
    <?php
        zerif_top_single_post_trigger();
    ?>
        <div id="primary" class="content-area">
                <main itemscope itemtype="http://schema/WebPageElement" itemprop="mainContentOfPage" id="main" class="site-main">
    <?php
        while (have_posts()) {
            the_post();
            get_template_part('content', 'single');
            zerif_post_nav();
            /* If comments are open or we have at least one comment, load up the comment template */
            if (comments_open() || '0' != get_comments_number()) {
                comments_template('');
            }
        }
    ?>
            </main><!-- #main -->
            </div><!-- #primary -->
    <?php
        zerif_bottom_single_post_trigger();
    ?>
    </div><!-- .content-left-wrap -->
    <?php
        zerif_after_single_post_trigger();

        if ($sidebar_layout === 'sidebar-right' && empty($zerif_change_to_full_width) || is_singular('post')) {
            zerif_sidebar_trigger();
        }
    ?>
</div><!-- .container -->
    </div>
    <?php
}
get_footer();
?>
发布评论

评论列表(0)

  1. 暂无评论