Labs64 / credit-tracker

Credit Tracker WordPress plugin
https://wordpress.org/plugins/credit-tracker/
GNU General Public License v2.0
20 stars 20 forks source link

Photo caption table for attachments to current post #17

Open r-brown opened 9 years ago

r-brown commented 9 years ago

How would I construct the shortcode so it displays the photo credit table for all photos attached to the current post? currently, i'm implementing the shortcode in a sidebar on all blog pages.

remove_shortcode( 'credit_tracker_table' );
    add_shortcode( 'credit_tracker_table', 'my_credit_tracker_table_shortcode' ) );

    function my_credit_tracker_table_shortcode( $atts ) {
        extract( shortcode_atts( array(
             'attachments' => false
        ), $atts ) );
        if ( $attachments ) {
            global $post;
            $attachments    = get_attached_media( 'image' );
            $attachment_ids = array();
            foreach ( $attachments as $attachment ) {
                $attachment_ids[] = $attachment->ID;
            } //$attachments as $attachment
            $attachment_ids = implode( ',', $attachment_ids );
            $atts[ 'id' ]   = $attachment_ids;

        } //$attachments
        return ( credit_tracker_table_shortcode( $atts ) );
    }

Source: https://wordpress.org/support/topic/photo-caption-table-for-attachments-to-current-post

Jaace commented 7 years ago

I can look into this!

r-brown commented 7 years ago

Hi @Jaace - absolutely, thank you for your support!

Jaace commented 7 years ago

About to push up a commit. A few notes - if the image isn't attached to anything, it won't show, so make sure it's actually attached to the post you expect.

Wherever the shortcode is included (widget or in the post_content), the post ID is used, so if it's in the sidebar, it will be relative to the page being viewed.

This brought to light an issue where if the image doesn't have any credit tracker meta saved, it doesn't show in the table at all, but the table could still show the headers. This is because the table only checks if the image exists, but not if the meta exists for the image. This could be another ticket as it's not directly related to this fix.