• Resolved Suhan Alam Rana

    (@ranakpik)


    Hello, and thank you for the excellent plugin. I want to show custom post meta data without having to install the AFC plugin. I’ve created the meta box using custom functions, but the global $post variable might not be accessible. Can you please advise on how I can achieve this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael

    (@migaweb)

    Hi, what do you mean by “might not be available”? It is a WP global variable:

    $post (WP_Post) The post object for the current post. Object described in Class_Reference/WP_Post.

    https://codex.www.ads-software.com/Global_Variables

    That shouldn’t have anything todo with ACF or not.

    Thread Starter Suhan Alam Rana

    (@ranakpik)

    Do you have any idea why the metadata isn't appearing on the custom post details template?
    
    
    function display_custom_post_meta_shortcode( $atts ) {
    
    ? ? // Extract shortcode attributes
    
    ? ? $atts = shortcode_atts( array(
    
    ? ? ? ? 'post_id' => get_the_ID(), // Default to current post ID if not specified
    
    ? ? ? ? 'meta_key' => '', // Specify the meta key
    
    ? ? ? ? 'before' => '', // Text to display before the meta value
    
    ? ? ? ? 'after' => '', // Text to display after the meta value
    
    ? ? ), $atts );
    
    ? ? // Check if the meta key is specified
    
    ? ? if ( empty( $atts['meta_key'] ) ) {
    
    ? ? ? ? return ''; // Return empty string if meta key is not specified
    
    ? ? }
    
    ? ? // Get the meta value for the specified meta key
    
    ? ? $meta_value = get_post_meta( $atts['post_id'], $atts['meta_key'], true );
    
    ? ? // Output the meta value with before and after text
    
    ? ? if ( $meta_value ) {
    
    ? ? ? ? return $atts['before'] . $meta_value . $atts['after'];
    
    ? ? } else {
    
    ? ? ? ? return ''; // Return empty string if meta value is not found
    
    ? ? }
    
    }
    
    add_shortcode( 'custom_post_meta', 'display_custom_post_meta_shortcode' );
    
    // [custom_post_meta meta_key="project_date" before="Meta Value: "]
    
    
    
    
    Plugin Author Michael

    (@migaweb)

    No, sorry. Maybe it is getting the ID of the template page and not the content page. Check the files in the widgets/ folder of my plugin (e.g. the PostACF file) to see how I read the content of the pages.

    But if you use custom code it is not part of this plugin so you have to solve that yourself. Hope you can figure it out.

    Thread Starter Suhan Alam Rana

    (@ranakpik)

    Oh, okay. Thank you for your time!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying custom metadata without using the AFC plugin.’ is closed to new replies.