1

Reference Post (I was even downvoted here.)

My short code was not working on my custom theme, but the same short code, when used in twenty fifteen, was working.

I researched over google and found that it was something related to not using the_content correctly.

  1. https://kb.yoast.com/kb/shortcodes-not-rendering/
  2. https://wordpress.org/support/topic/shortcodes-not-working-custom-theme/

I applied the solution suggested in the Yoast →

that means I changed from

<?php the_content(); ?>

TO

<?php echo apply_filters( 'the_content', $post->post_content ); ?>

But still, the shortcodes are not working.

what is the working fix?

I have this code in single.php →

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                            <?php $post_id = get_the_ID(); ?>
                            <?php get_template_part('content',get_post_format()); ?>
                        <?php endwhile; ?>
                        <?php endif; ?>

and in content.php →

<?php the_content(); ?>

enter image description here

ON LIVE PAGE→

enter image description here

FULL SHORTCODE →

function simplisto_the_image($atts) {
    $atts = shortcode_atts( array(
        'to' => 'https://pics.wikifeet.com/Melania-Trump-Feet-720891.jpg'
    ), $atts);
    $output = '<div class="lazyimg">';
            $output .= '<img class="lazyimg-popup" src="'.$atts['to'].'" alt="The First Caption" width="100%" height="auto">';
        $output .= '<i class="fa fa-expand" aria-hidden="true"></i>';
    $output .= '</div>';
    return $output;
}

function register_shortcodes(){
   add_shortcode('simage', 'simplisto_the_image');
}

add_action( 'init', 'register_shortcodes');
  • Try echo do_shortcode( get_the_content() ); – user125500 Aug 8 '17 at 18:47
  • 1
    You said you changed the_content() to apply_filters, but then you say content.php has the_content().... it should have apply_filters no? – gdaniel Aug 8 '17 at 19:12
  • 1
    what is the exact full code of your shortcode function? does the same problem exist when you use one of the generic shortcodes like [gallery]? – Michael Aug 8 '17 at 21:58
  • 1
    ps: are you using any postformat for the post? – Michael Aug 8 '17 at 22:03
  • 1
    Can you try to replace function register_shortcodes()...to the end with just add_shortcode('simage', 'simplisto_the_image'); – rudtek Aug 8 '17 at 22:25
2

Excuse brief answer (writing on train in iPhone) but I would try changing get_template_part() to its non-WordPress include() equivalent as, from memory, variables declared outside your content.php won't be available otherwise.

  • @Jack Johansson Any clue? – The WP Novice Aug 8 '17 at 20:48
  • even if I am trying this in content.php → <?php echo do_shortcode("[simage]"); ?> still it is not generating the DESIRED OUTPUT. there is something else that is fundamentally missing in the theme. I have no clue what is that? – The WP Novice Aug 8 '17 at 21:23
  • Please guide me one step further sir. Please help. – The WP Novice Aug 9 '17 at 14:17
  • I have accepted the answer. – The WP Novice Oct 5 '17 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.