• Resolved Ariyan

    (@ariyan0282)


    I have two shortcodes. I want to show them in another.

    I have one shortcode “[post type=”content” id=”2″] and another is [post_id]. I want something like [post type=”content” id=[post_id]]. Because I am not getting any other way of showing the current post content using a shortcode.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator t-p

    (@t-p)

    See if the following thread points in the right direction: https://wordpress.stackexchange.com/questions/35208/shortcode-inside-another-shortcode

    Alor UX

    (@jerrymayalor555)

    Hi @ariyan0282,

    You can try the code below and let me know if it works for you.

    function custom_post_shortcode($atts) {
        // Extract shortcode attributes
        $atts = shortcode_atts(
            array(
                'type' => 'content',
                'id'   => '',
            ),
            $atts,
            'custom_post'
        );
    
        // Check if [post_id] exists
        if (shortcode_exists('post_id')) {
            // Get the value of [post_id]
            $post_id = do_shortcode('[post_id]');
            
            // Set the id attribute to the value of [post_id]
            $atts['id'] = $post_id;
        }
    
        // Use the attributes to generate the desired output
        $output = "[post type='{$atts['type']}' id='{$atts['id']}']";
        
        return $output;
    }
    
    // Register the custom shortcode
    add_shortcode('custom_post', 'custom_post_shortcode');
    

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to show one shortcode into another’ is closed to new replies.