Forum

Thread tagged as: Question

If region is empty use another regions content.

Is it possible to check if a region is empty, then if so use content from a different region?

Tony Astley

Tony Astley 0 points

  • 4 years ago
If (!perch_content('region', true) perch_content('other_region')

I can't figure that out, maybe the code might help explain what I'm trying to achieve:

<div class="imageSlides">
    <?php 
        perch_content_create('Artwork Slideshow', array(
            'template' => '_artwork/_artwork_slide.html',
            'multiple' => true,
            'add-to-top' => true,
        ));
        perch_content('Artwork Slideshow');
    ?>
</div><!--/.imageSlides-->
<div class="thumbs">
    <ul>
        <?php 
            perch_content_create('Artwork Images', array(
                'template' => '_artwork/_artwork_image.html',
                'multiple' => true,
                'add-to-top' => true,
            ));
            perch_content('Artwork Images');
        ?>                      
    </ul>
</div><!--/.thumbs-->

I want to check if the client has populated the 'Artwork Slideshow' region, and if not use the images they have populated the 'Artwork Images' with instead.

Is this possible. The code is taken from the /perch/templates/pages/artwork.php template.

<div class="imageSlides">
<?php 
perch_content_create('Artwork Slideshow', array(
'template' => '_artwork/_artwork_slide.html',
'multiple' => true,
'add-to-top' => true,
));
$slideshow = perch_content('Artwork Slideshow', true);
if ($slideshow) echo $slideshow;
?>
</div><!--/.imageSlides-->
<div class="thumbs">
<ul>
<?php 
perch_content_create('Artwork Images', array(
'template' => '_artwork/_artwork_image.html',
'multiple' => true,
'add-to-top' => true,
));
if (!$slideshow) perch_content('Artwork Images');
?> 
</ul>
</div><!--/.thumbs-->

I'm not going to redo your entire code block, but I have added the checks you asked for.

It's not the best approach, but its a working example based on your supplied example.

Thanks Robert,

That's almost there, it's successfully replaced the empty region. However it no longer shows the artwork images region in the template afterwards. I need both regions to be displayed with one copying substitute content from the other if empty.

Is this possible?

Hello Robert,

I've figured it out using your method of storing the region as a standard php variable. Many thanks.

For anyone with a similar issue, I declared the regions at the top of the pages template and stored them as PHP variables:


<?php perch_content_create('Artwork Slideshow', array( 'template' => '_artwork/_artwork_slide.html', 'multiple' => true, 'add-to-top' => true, )); perch_content_create('Artwork Images', array( 'template' => '_artwork/_artwork_image.html', 'multiple' => true, 'add-to-top' => true, )); $slideshow = perch_content('Artwork Slideshow', true); $images = perch_content('Artwork Images', true); ?>

Then used an if statement to check for content as required with a fallback:

<?php echo !empty($slideshow) ? $slideshow: $images; ?>

Then echoed the artwork region wherever required:

<?php echo $images; ?>

Thanks again Robert.

Regards Tony