• Resolved antonie7

    (@antonie7)


    Hi,

    Mayby I’m overlooking something, but it seems my custom post type loop doesn’t support the use of shortcodes. Here’s my php:

    
    
    <?php
    
    $args = array(
        'post_status' => 'publish',
        'post_type' => 'sections',
        'meta_key' => 'display_order',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'showposts' => '50',
    );
    
    $posts = get_posts($args);
    foreach($posts as $post): ?>
    
    <section class='segment-wrapper'  style="background-color:<?php the_field('bgcolor'); ?>;color:<?php the_field('txcolor'); ?>;">
    
    <div class='text-inside'>
    
    <?php echo ($post->post_content); ?>
    
    </div>
    </section>
    
    <?php endforeach; ?>
    
    

    So, this loop works fine except for the shortcodes used in the content of the returned posts.

    Any suggestions? Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    Use the following for displaying contents,
    echo do_shortcode( $content );

    A little different from what @zaheerd says, I’m also wondering why you use

    <?php echo ($post->post_content); ?>

    and why you’re not using

    <?php the_content(); ?>

    instead. For better performance, I recommend taking a look at the code structure here (WP codex) and mimicking it. Definitely use wp_reset_postdata() If you’re going further with this template, you could be missing out on nice WP built-ins by not using setup_postdata().

    Moderator bcworkz

    (@bcworkz)

    If you want to use the_content() instead, because you are not using a standard WP loop, you need to use setup_postdata() before using any template tags like the_content(), the_title(), etc.

    do_shortcode() will expand shortcodes, but post content is filtered for more than just shortcodes. If you want to ensure output is just like what we see from a standard WP loop, either use the_content() or do something like echo apply_filters('the_content', $post->post_content );

    Thread Starter antonie7

    (@antonie7)

    Thanks for clarifying! Using <?php echo ($post->post_content); ?> was a rookie mistake on my part, I guess.
    The loop works perfectly now using the suggested syntax.

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

The topic ‘Post loop and shortcodes’ is closed to new replies.