0

I am building a static landing page on WordPress root folder, and I need to import some shortcodes from an existing page located in wp-content directory.

What I do is import with $post = get_post($post_id) the post containing my shortcode, then I extract it ->

$output = apply_filters( 'the_content', $post->post_content );

Finally I do ->

echo do_shortcode($output);

The issue here is that on the page I got displayed only the raw format of the shortcode, like [shortcode];

There is a way to convert shortcodes into plain html so my page can display it correctly or I am missing some require/include somewhere? I already added define( 'WP_USE_THEMES', false ); require "wp-load.php";

0

First of all, you have a syntax error. If your shortcode is stored in the $output variable, you would need to call echo do_schortcode($output);.

But, wouldn't simply the_content(); do the trick? I believe the shortcodes would be processed if you do this.

You can make a new query with WP_Query, and then use the_content() in the loop.

$args = array(
  'post_type' => 'post', // This selects only posts
  'post_status' => 'publish' // Only published posts
);
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post(); ?>
        <li><?php the_content(); ?></li>
    <?php }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}
  • I update my question, that was a typo :) however, where should I place the_content()? I am totally new to wordpress, even id I know that is a basic wordpress function I would like to know where to place it in my code – Alessandro Biagini Sep 12 '18 at 13:54
  • I've updated my answer, see if that works for you. – Pim Sep 12 '18 at 14:02
0

For other who wonders about it: I resolved adding WPBMap::addAllMappedShortcodes(); to my function, which renders all shortcodes!

define( 'WP_USE_THEMES', false ); 
require_once "wp-load.php";
require_once "wp-content/themes/salient/functions.php";
/*
Function that gets an object containing post data
and returns in output the post_content value.

Return string.
*/
function get_post_content(){

    //renderize shortcode
    WPBMap::addAllMappedShortcodes();

    $post = get_post(INSERT_POST_NUMBER);
    $output = apply_filters( 'the_content', $post->post_content );
    $value = do_shortcode($output);
    return $value;
}

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.