如何在wordpress之外获得短代码?

时间:2014-01-30 23:19:28

标签: wordpress plugins wordpress-plugin shortcode

如何在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 ......

2 个答案:

答案 0 :(得分: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];
    };
?>

答案 1 :(得分:0)

这是可能的。有一个编写器包,允许您在任何PHP应用程序中创建类似短代码的wordpress:https://github.com/maiorano84/shortcodes

您可以在此处找到教程:https://www.codediesel.com/php/adding-wordpress-like-shortcodes-to-your-web-applications/