短代码显示短代码而不是内容

时间:2015-03-27 07:56:58

标签: wordpress wordpress-plugin

我正在使用"插入页面" wordpress的插件,允许我将页面和帖子插入到后端的其他页面和帖子中。我的问题是,当我使用"插入页面"插件在我的新闻部分,然后访问该网站,它显示短代码而不是内容。这似乎只是在制作" post"时才会出现问题。如果我使用插件在另一个页面中显示页面,它可以正常工作(显示内容而不是短代码)。

知道这个问题的原因是什么?

干杯, 埃米尔

更新

// Shortcode hook: Replace the [insert ...] shortcode with the inserted page's content
    function insertPages_handleShortcode_insert( $atts, $content = null ) {
        global $wp_query, $post, $wp_current_filter;
        extract( shortcode_atts( array(
                    'page' => '0',
                    'display' => 'all',
                ), $atts ) );

        // Validation checks.
        if ( $page === '0' ) {
            return $content;
        }

        // Trying to embed same page in itself.
        if ( $page == $post->ID || $page == $post->post_name ) {
            return $content;
        }

        // Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
        $done = false;
        foreach ( $wp_current_filter as $filter ) {
            if ( 'the_content' == $filter ) {
                if ( $done ) {
                    return $content;
                } else {
                    $done = true;
                }
            }
        }

        // Get page object from slug or id
        $temp_query = clone $wp_query; // we're starting a new loop within the main loop, so save the main query
        $temp_post = $wp_query->get_queried_object(); // see: http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2

        // Convert slugs to page IDs to standardize query_posts() lookup below.
        if ( ! is_numeric( $page ) ) {
            $page_object = get_page_by_path( $page, OBJECT, get_post_types() );
            $page = $page_object ? $page_object->ID : $page;
        }

        if ( is_numeric( $page ) ) {
            $args = array(
                'p' => intval( $page ),
                'post_type' => get_post_types(),
            );
        } else {
            $args = array(
                'name' => esc_attr( $page ),
                'post_type' => get_post_types(),
            );
        }

        query_posts( $args );

        // Start our new Loop
        while ( have_posts() ) {
            ob_start(); // Start output buffering so we can save the output to string

            // Show either the title, link, content, everything, or everything via a custom template
            // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
            // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
            // are only getting called once. The fix here is to disable processing of filters on the_content in
            // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
            switch ( $display ) {
            case "title":
                the_post(); ?>
                <h1><?php the_title(); ?></h1>
                <?php break;
            case "link":
                the_post(); ?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php break;
            case "excerpt":
                the_post(); ?>
                <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
                <?php break;
            case "excerpt-only":
                the_post(); ?>
                <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
                <?php break;
            case "content":
                the_post(); ?>
                <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_content(); else the_content(); ?>
                <?php break;
            case "all":
                the_post(); ?>
                <h1><?php the_title(); ?></h1>
                <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_content(); else the_content(); ?>
                <?php the_meta(); ?>
                <?php break;
            default: // display is either invalid, or contains a template file to use
                $template = locate_template( $display );
                if ( strlen( $template ) > 0 ) {
                    include $template; // execute the template code
                } else { // Couldn't find template, so fall back to printing a link to the page.
                    the_post(); ?>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
                }
                break;
            }

            $content = ob_get_contents(); // Save off output buffer
            ob_end_clean(); // End output buffering
        }
        wp_reset_postdata();
        $wp_query = clone $temp_query; // Restore main Loop's wp_query
        $post = $temp_post;

        $content = "<div data-post-id='$page' id='insertPages_Content'>$content</div>";
        return $content;
        //return do_shortcode($content); // careful: watch for infinite loops with nested inserts
    }

1 个答案:

答案 0 :(得分:0)

这可能对你有用。

while ( have_posts() ) {
        ob_start(); // Start output buffering so we can save the output to string

        // Show either the title, link, content, everything, or everything via a custom template
        // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
        // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
        // are only getting called once. The fix here is to disable processing of filters on the_content in
        // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
        switch ( $display ) {
        case "title":
            the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <?php break;
        case "link":
            the_post(); ?>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php break;
        case "excerpt":
            the_post(); ?>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
            <?php break;
        case "excerpt-only":
            the_post(); ?>
            <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo get_the_excerpt(); else the_excerpt(); ?>
            <?php break;
        case "content":
            the_post(); ?>
            <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo do_shortcode( get_the_content() ); else the_content(); ?>
            <?php break;
        case "all":
            the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <?php if ( has_filter( 'the_content', 'sharing_display' ) ) echo do_shortcode( get_the_content() ); else the_content(); ?>
            <?php the_meta(); ?>
            <?php break;
        default: // display is either invalid, or contains a template file to use
            $template = locate_template( $display );
            if ( strlen( $template ) > 0 ) {
                include $template; // execute the template code
            } else { // Couldn't find template, so fall back to printing a link to the page.
                the_post(); ?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
            }
            break;
        }