Thanks for the kind words about the plugin and the documentation (and for taking the time to read through it all). You’re pretty brave to jump into PHP and page templates and all that.
There have been a few earlier support topics regarding PHP and image attachment pages; you may find them helpful in general:
building-an-archive-gallery-with-mla
category-template-please-help
With regard to using custom fields in this context, there are two approaches you can take; 1) add an [mla_gallery]
to the page and use the custom:
prefix to access the fields, or 2) access the fields with WordPress API calls in the PHP code and use PHP to compose the page.
The first approach is along the lines of your “do_shortcode … ” idea. You would compose a gallery with the single image and use the Gallery Display Content parameters or a custom markup template to display the fields you want. For example:
<br />
do_shortcode( sprintf( '[mla_gallery ids="%1$s" columns=1 size=full mla_caption="{+custom:Name+}, {+custom:City+}"]', get_the_ID() ) );<br />
To make the caption into a link you’d code something like:
do_shortcode( sprintf( '[mla_gallery ids="%1$s" columns=1 size=full mla_caption=\'<a href="{+custom:Twitter+}>My Twitter Link</a>\']', get_the_ID() ) );
That assumes the content of the Twitter
custom field is a valid URL. There’s no special coding for fields containing URLs; you add the HTML for the link yourself.
If you want something more complex, like your “In more detail … “, example you can define a custom markup template like the “table-based template example” in the Settings/Media Library Assistant Documentation tab. The field delimiters are “[+ … +]” but the content is the same. The important points are that the MLA “custom:” prefix only works in the context of an [mla_gallery]
shortcode and that the shortcode returns the HTML for a complete gallery. You’d put your <div>
tags, etc. in the MLA markup template and run it through the shortcode. In the markup template you’d have something like:
<br />
<div class="name">[+custom:Name+]</div><br />
<div class="city">[+custom:City+]</div><br />
<div class="social">[+custom:Twitter+] | [+custom:LinkedIn+]</div><br />
All of the template content is in a single string, so there are no ‘text separators’ within the template. The .'|'.
notation you show is something that a PHP statement would require.
If you want to put the <div>
tags, etc., in your PHP for the page itself you would use the second approach to access the custom field contents. You would call a WordPress function to get the data you want:
Function Reference/get metadata
For example:
<br />
<?php<br />
$data = get_metadata( 'post', get_the_ID() ); // GET ALL THE DATA<br />
$name = $data['Name'];</p>
<p>// OR GET ONE FIELD<br />
$name = get_metadata( 'post', get_the_ID(), 'Name' );</p>
<p>echo '<div class="name">' . $name . '</div>';<br />
?><br />
For your follow up question on optional fields (which require PHP logic to suppress the separator for empty fields), you’d need something like:
<br />
<?php<br />
$data = get_metadata( 'post', get_the_ID() ); // get all the data<br />
$twitter = isset( $data['Twitter'] ) ? trim( $data['Twitter'] ) : '';<br />
$linkedin = isset( $data['LinkedIn'] ) ? trim( $data['LinkedIn'] ) : '';</p>
<p>if ( ! empty( $twitter ) ) {<br />
$the_link = '<a href="' . $twitter . '">Twitter</a>';<br />
}</p>
<p>if ( ! empty( $linkedin ) ) {<br />
if ( ! empty( $the_link ) )<br />
$the_link .= ' | ';</p>
<p> $the_link .= '<a href="' . $linkedin . '">LinkedIn</a>';<br />
}</p>
<p>echo '<div class="social">' . $the_link . '</div>';<br />
?><br />
That’s a lot of PHP to mull over. The isset()
function handles the case where the custom field does not exist at all. The trim()
function removes whitespace around the link. The empty()
function tests for anything left over. The .=
operator is a shorthand notation for concatenation.
I am traveling and don’t have access to my development system, so I haven’t tested any of the above code. I apologize for any typos, omissions or other errors. Give it a try and please do let me know if you have any problems or more questions.
Pagination, by the way, is a whole new ballgame. It’s very theme dependent and attachments are treated in a different way, not like posts/pages. When you get there, start another support topic and I’ll give you what help I can.
Thanks again for your interest in the plugin and these questions, which should be useful to other customers as well. Have fun!