2013-04-08 78 views
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'); 
+0

错误?输出? $内容是在哪里引用的? – 2013-04-08 11:01:28

+0

短代码的引用就像是在一个帖子里面[insert_page page =“154”] – 2013-04-08 11:03:30

+0

请把你如何在帖子中调用短代码。另外,从你的链接,你使用'do_shortcode'方法来测试它吗? – Jon 2013-04-08 11:03:45

回答

0

感谢你的帮助..简码似乎是现在的渲染(这个问题很可能错了function.php文件:))..

看来,主题有另一个功能。 PHP文件,该文件是使用..(不知道的主题做了一个副本或者被错误由开发完成.. :))..

问候

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; 
} 
+0

会尝试让知道..谢谢.. – 2013-04-08 18:08:46