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(''); ?>
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.
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: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.