0
votes

I'm creating a custom media center wordpress website.

I have created my own gallery, which I insert into my template via this...

<?php echo do_shortcode('[gallery customgal="true"]'); ?>


But however, on occasions, sometimes my post will have other attachments besides just images.

So I have added this shortcode, using the EG Attachments plugin, which adds other attachments into my post...

<?php echo do_shortcode('[attachments]'); ?>


This works fine. But above my attachment shortcode, I need a H tag title...

<h2>Videos</h2>

<?php echo do_shortcode('[attachments]'); ?>


But my problem is, if no attachments have been added to my post, then I'm stuck with a title and nothing below it!

Is it possible to only display my shortcode if attachments, other than images, exist?

Something like this...

<?php if (attachmentexists()) { ?>

     <h2>Other Attachments</h2>

     <?php echo do_shortcode('[attachments]'); ?>

<?php } ?>


Any help would be hugely appreciated thanks.

2

2 Answers

1
votes

You have the right idea. The best solution here would probably be to assign the result of <?php echo do_shortcode('[attachments]'); ?> to a variable, and then check to see if it's set:

<?php $attachments = do_shortcode('[attachments]'); ?>
<?php if( $attachments ): ?>
    <h2>Videos</h2>
    <?php echo $attachments; ?>
<?php endif; ?>

Alternatively, you can amend your shortcode to take a parameter, e.g. title [attachments title="Videos"], and perform the (same) logic from within the shortcode.

0
votes

I'm not quite sure what your question is yet, but change the way you output your PHP:

<?
     if (attachmentexists()) {
     echo "<h2>Videos</h2>";
     echo do_shortcode('[attachments]');
     } 
?>