Advice: Template structure – php blocks in sidebar or someting else?

I’m trying to create certain modulair blocks which I can add to multiple pages in WordPress. But I use sidebar-[name].php files for this, what is the best practice for this? Should you add them to a function or add them in a sidebar…

It feels Kinda off using many sidebar’s. It would be nice if I could add them to a sub-folder in the template. Checkout the image. How do you do this?

Read More

enter image description here

Related posts

2 comments

  1. I would say to add them as template parts as in content-[name].php. For instance in page.php you could have something like:

    <?php while ( have_posts() ) : the_post(); ?>
                <?php if (is_page( 'home' ) ){
                     get_template_part( 'content', 'benefits' );
                    } elseif (is_page( 'Contact' ) ){
                     get_template_part( 'content', 'contact' ); 
                    }  
                 } 
    
                get_template_part( 'content', 'popup' ); ?>
            <?php endwhile; // end of the loop. ?>
    

    It seems to make more sense as it is content after all.

  2. Who says you can not put template files into sub-directories?

    Your custom-templates can be put wherever you want them to be – because it is you who includes them, anyway.

    If you see the Codex on get_template_part, you find the following:

    get_template_part( 'partials/content', 'page' );
    

Comments are closed.