I am building a shortcode that displays a list of attached pdfs to a custom post type. This php code has been provided by your support about a year ago and it works, except the output starts with an empty list item
/* SHORTCODE -- Project Files Attachments
==============================================*/
function files_casestudy_shortcode($atts) {
extract(shortcode_atts(array(
'attachment' => '',
), $atts ));
global $post, $wpdb;
$story_files = get_post_meta($post->ID, 'wpcf-file-case-study', false);
if(!empty($story_files)) {
foreach ($story_files as $story_file) {
$attachments[] = $story_file;
}
}
if (!empty($attachments)) {
$out = '';
foreach ($attachments as $attachment) {
$attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = '%s';", $attachment));
$attachment_title = get_the_title($attachment_id);
$caption = get_the_excerpt($attachment_id);
$description = get_the_content($attachment_id);
$attachment_url = wp_get_attachment_url($attachment_id);
$out .= '<li>';
$out .= '<a href="' . $attachment_url . '" title="' . $attachment_title . '" target="_blank" class="attachment-link">'.$attachment_title.'</a>';
$out .= '</li> ';
}
}
$out .= '';
return $out;
}
add_shortcode('files_casestudy-attachments', 'files_casestudy_shortcode');
<!-- wpv-loop-start -->
<ul class="wpv-loop js-wpv-loop">
<wpv-loop>
<li>
[files_casestudy-attachments]
</li>
</wpv-loop>
</ul>
<!-- wpv-loop-end -->
Post ID filter
Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]
Output in page template
[wpv-conditional if="( $(file-case-study) ne '' )"][wpv-view name="case-studies-display-project-files" ids="[wpv-post-id]"][/wpv-conditional]
gives
<ul class="wpv-loop js-wpv-loop">
<li></li>
<li><a href="[deleted]/wp-content/uploads/2018/10/Annex-1-Lime-stabilisation.docx" title="Annex 1 – Lime stabilisation" target="_blank" class="attachment-link">Annex 1 – Lime stabilisation</a></li>
<li><a href="[deleted]/wp-content/uploads/2018/10/Annex-2-Unplanted-drying-beds.docx" title="Annex 2 – Unplanted drying beds" target="_blank" class="attachment-link">Annex 2 – Unplanted drying beds</a></li>
<li><a href="[deleted]/wp-content/uploads/2018/10/Annex-3-Break-down-operational-costs.docx" title="Annex 3 – Break-down operational costs" target="_blank" class="attachment-link">Annex 3 – Break-down operational costs</a></li>
<li><a href="[deleted]/wp-content/uploads/2018/10/Annex-4-Break-down-investment-costs.docx" title="Annex 4 – Break-down investment costs" target="_blank" class="attachment-link">Annex 4 – Break-down investment costs</a></li>
<li><a href="[deleted]/wp-content/uploads/2018/10/Annex-5-Land-use-specifications.docx" title="Annex 5 – Land use specifications" target="_blank" class="attachment-link">Annex 5 – Land use specifications</a></li>
</ul>
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Kristof,
As you say the first index is returning an empty item.
Could you modify the code a bit for some debugging.
Add the following var_dump($attachments); right above the second foreach statement. Like this.
if (!empty($attachments)) {
$out = '';
var_dump($attachments);
foreach ($attachments as $attachment)
Please send me a screenshot of what exactly is displayed on the page.
Thanks,
Shane
I have added the code:
array(5) { [0]=> string(90) "hidden link " [1]=> string(93) "hidden link " [2]=> string(100) "hidden link " [3]=> string(99) "hidden link " [4]=> string(95) "hidden link " }
I have also uploaded a screenshot.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Kristof,
Based on this it seems that all the items are being built correctly based on the loop.
I would need to do some further investigations on this for you.
Would you mind providing admin access to the site for me to check on this one ? Also please send me a link to the page where this code is running
Thanks,
Shane
yes, please turn on privacy mode I will provide login.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Kristof,
Here are the private fields.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Kristof,
I found the issue. It was that the code was already generating an
tag around the items so all that was needed was to remove the one that was wrapping the shortcode.
Thanks,
Shane
My issue is resolved now. Thank you!