• I have created shortcode for posts, now point is i need to shortcode post in post/page which works. Example i embed post2 in post1 and when i visit post1 i see that post2, but when i embed post1 in page1 i dont see post2

    This is code i have written so far.

    
    function getPostShortcode( $atts, $content = '' ) {
    	extract( shortcode_atts( array(
    		'id'    => '',
    		'title' => ''
    	), $atts, 'post_shortcode' ) );
    
    	if ( empty( $atts['id'] ) )
    		return;
    
    	$loop = new WP_Query( array(
    		'post_type' => 'post',
    		'p'         => $atts['id']
    	) );
    	ob_start();
        if ( $loop->have_posts() ) {
    		while ( $loop->have_posts() ) : $loop->the_post();
    						$desc  = ! empty( $atts['desc'] ) ? $atts['desc'] : get_the_excerpt();
            ?>
    			<div class="post-single-shortcode-aka">
                    <h2><a href="#"><?php echo $title; ?></a></h2>
                    <p><?php echo $desc; ?></p>
    			</div>
            <?php 
            endwhile;
            wp_reset_postdata(); 
        } 
    	return ob_get_clean();
    }
    add_shortcode( 'post_shortcode', 'getPostShortcode' );
    • This topic was modified 6 years, 7 months ago by armin1997.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    So essentially your shortcode works fine in post content but not page content? What happens on pages? Does the shortcode itself appear? Or is there simply nothing?

    Incidentally, it doesn’t appear that $title is assigned a value anywhere, so echo $title; would not output anything. Maybe you should use the_title();?

    If you haven’t already done so, define WP_DEBUG as true in wp-config.php so you can see any PHP errors that occur which might prevent proper output.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress shortcodes’ is closed to new replies.