0

インターネットからのコード サンプルを使用して、特定のページを返すショートコードを作成しました。ただし、ショートコードはレンダリングされていないようですが、ショートコードのテキストがそのまま表示されます。関数は 内で定義されていfunctions.phpます。

コードは次のとおりです。

/* Code taken from: 
 * http://alex.leonard.ie/2010/09/09/wordpress-shortcode-to-insert-content-of-another-page/
 */

function pa_insertPage($atts, $content = null) {
     // Default output if no pageid given
     $output = NULL;
     // extract atts and assign to array
     extract(shortcode_atts(array(
     "page" => '' // default value could be placed here
     ), $atts));
     // if a page id is specified, then run query
     if (!empty($page)) {
     $pageContent = new WP_query();
     $pageContent->query(array('page_id' => $page));
     while ($pageContent->have_posts()) : $pageContent->the_post();
     // assign the content to $output
     $output = get_the_content();
     endwhile;
     }
     return $output;
}
add_shortcode('insert_page', 'pa_insertPage');
4

2 に答える 2

1

質問に対する回答ではなく(問題はコードではなく別の場所にあります)、コードの最適化されたバージョンです。コミュニティ Wiki モードで公開するため、反対票や賛成票による意図しない評判はカウントされません。

add_shortcode( 'insert_page', 'insert_page_so_15877376' );

function insert_page_so_15877376( $atts, $content = null ) 
{
    // Default output if no pageid given
    $output = '';

    // Access $atts directly, no need of extracting
    if( !isset( $atts['page'] ) )
        return $output;

    // Grab the page directly, no need of WP_Query
    // get_post() could be used as well
    $get_page = get_page( $atts['page'] );
    if( !$get_page )
        return $output;

    // Do Shortcode in case the other page contains another shortcode
     $output = do_shortcode( $get_page->post_content );
     return $output;
}
于 2013-04-08T12:24:37.023 に答える
0

助けてくれてありがとう..ショートコードは現在レンダリングされているようです(問題はおそらく間違ったfunction.phpファイルでした:))..

テーマには、使用される別の functions.php ファイルがあったようです.. (テーマが複製コピーを作成したのか、開発者によって誤って作成されたのかはわかりません.. :) ) ..

よろしく

于 2013-04-15T06:02:22.577 に答える