Search code examples
phpwordpresscustom-post-type

Custom sections per custom post type file


So I'm trying to achieve this:

I've created custom post types and there is one custom post type where I want variable sections in. That post type is "workarea". There are four posts (main pages) for that custom post type.

At the bottom of those pages, there is a related posts section.

Each of those, should show different related posts. But, I use the "single-workarea.php" as the custom post type template. So there is no way of (at least that I know of) to create a loop that shows the different related posts, that should be displayed per page.

If I edit that loop, it applies to (obviously) all pages from the custom post type "workarea".

Is there a way to create a variable/custom section in that post type php file?

A solution is that I remove that post type, and just create pages with template names, such as "workarea", but the problem with that is, is whenever my customer wants to add a new post type "workarea", he has to come to me so I can create a new php file.

I hope I made a clear explanation.

Thank you in advance!


Solution

  • So I've solved this problem with the Advanced custom fields taxonomy field.

    This is the loop now:

    <?php
            $relevant = get_field('relevante_posts_categorie');
    
            $postid = get_the_ID();
            $loop = new WP_Query( array(
                'post_type' => 'actueel',
                'posts_per_page' => 3,
                'category__in' => $relevant,
                'post__not_in'=> array($postid),
              )
            );
            ?>
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
              <a href="<?php the_permalink();?>">
                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relevant__item">
                  <div class="thumbnail__wrapper">
                    <div class="thumbnail" style="background-image:url('<?php the_field('thumbnail');?>');"></div>
                  </div>
                  <div class="title">
                    <h4><?php the_title();?></h4>
                  </div>
                </div>
              </a>
            <?php endwhile; wp_reset_query(); ?>
    

    The taxonomy field get's the category I select. Now it's customizable which posts I want to display. The only thing left to do is create multiple query loops where I define the custom post types.

    Thank you for your help.