2014-01-30 31 views
0

我如何在wordpress以外的地方獲得簡碼?如何在WordPress之外獲得簡碼?

這是我的代碼

<?php 
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); 
$args = array(
    'posts_per_page' => 10 // Specify how many posts you'd like to display 
); 
$latest_posts = new WP_Query($args); 
if ($latest_posts->have_posts()) { 
    while ($latest_posts->have_posts()) { 
     $latest_posts->the_post(); 
if (preg_match_all ("#\[scode\](.+)\[\/scode\]#", the_content(), $matches, PREG_SET_ORDER)) { 
echo $matches[1]; 
}; 
?> 

,這是我的內容

[scode]hare is my shortcode[/scode] 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, 

when an unknown printer took a galley of type and scrambled it to make a type specimen book 

任何人都可以幫助... PLZ

+0

只需撥打該簡碼掛的功能和它傳遞的參數,你通常會通過簡碼來完成。更好的問題是,爲什麼你需要在WordPress之外顯示這些帖子?我敢打賭,你可以創建一個自定義模板來滿足你的需求。 –

回答

0

如果我正確理解你的問題,你應該使用do_shortcode('[shortcode_name]')。它返回一個字符串,所以你把它叫做:<?php echo do_shortcode('[shortcode_name]'); ?>

在你的榜樣,你也許可以這樣做:

<?php 
    require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); 
    $args = array(
     'posts_per_page' => 10 // Specify how many posts you'd like to display 
    ; 
    $latest_posts = new WP_Query($args); 
    if ($latest_posts->have_posts()) { 
     while ($latest_posts->have_posts()) { 
      $latest_posts->the_post(); 
    if (preg_match_all ("#". do_shortcode('[scode]') ."(.+)". do_shortcode('[scode]') ."#", the_content(), $matches, PREG_SET_ORDER)) { 
    echo $matches[1]; 
    }; 
?> 
+0

我不想使用do_shortcode, 我在我的插件中使用此代碼= add_shortcode('scode','my_function'); 現在我怎麼能得到這個代碼scode? – user3255343