• Resolved dougshuffield

    (@dougshuffield)


    I am displaying a custom post type (The Events Calendar event) using the Post Carousel using a simple Post Template I have created. Inside the post template I am trying to use the following shortcode to display the formatted start date of the event:

    add_shortcode( 'ccc_event_date_and_time', 'event_date_and_time' );
    function event_date_and_time( $atts ) {
    return do_shortcode("[tribe_formatted_event_date id=".get_the_ID()." format=\"F j, Y, \a\t g:ia T\"]");
    }

    However, the get_the_ID() returns the page ID the carousel is on instead of the ID of the event. I have also tried $post->ID and other methods, but they all return the page ID. How might I be able to access the ID of the post the carousel is displaying?

    I am using WordPress 6.4.3 with the Twenty Twenty-Four theme and GetWid v2.0.5. Thanks for any help you can provide.

    Doug

Viewing 1 replies (of 1 total)
  • Thread Starter dougshuffield

    (@dougshuffield)

    I ran across this post which helped me reach the solution I was going for:

    https://www.ads-software.com/support/topic/custom-field-block-hows-date-not-formated-from-an-acf-field/

    I was unable to resolve the issue above, but this gives the same result I was attempting. I modified getwid/includes/templates/template-parts/post-custom-field.php like so:

    <?php
    
    //extract styles & classes
    extract($extra_attr);
    ?>
    
    <div class="<?php echo esc_attr( $wrapper_class ); ?>" <?php if ( !empty($wrapper_style) ) { ?> style="<?php echo esc_attr($wrapper_style); ?>" <?php } ?>>
        <?php if ( !empty($attributes['customField'] ) ) {
            // Original Code:
            //echo get_post_meta( get_the_ID(), esc_attr( $attributes['customField'] ), true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    
            // My code:
            $cfield=get_post_meta( get_the_ID(), esc_attr( $attributes['customField'] ), true );
            // Check if this is '_EventStartTime' custom field
            if ( $attributes['customField'] == '_EventStartDate' ) {
               // Reformat the date
               $date = DateTime::createFromFormat('Y-m-d H:i:s', $cfield)->format('F j, Y \\a\\t g:ia');
               echo $date;
            } else {
               // Just echo the custom field like normal
               echo $cfield;
            }
        } ?>
    </div>

    Just verified the custom field I wanted to format and then reformatted it to my desired format.

Viewing 1 replies (of 1 total)
  • The topic ‘Can’t access the post ID of custom post type in the Post Carousel via shortcode’ is closed to new replies.