在WP快捷代码中运行PHP结果

时间:2015-09-21 作者:O O\'Neill

如何将以下结果放入WP短代码中?

PHP Results:

    <?php

 function endsWith($haystack, $needle) {
     // search forward starting from end minus needle length characters
     return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
 }

 $lastDate = date(\'Y-m-d\', strtotime(\'today - 30 days\'));
 $todayDate = date(\'Y-m-d\');

 $results = $wpdb->get_results( "SELECT * FROM `ucsite_statistics_pages` WHERE id=1428 AND `date` BETWEEN \'$lastDate\' AND \'$todayDate\' ", ARRAY_A );
    for($i=0;$i<count($results);$i++)
    if (endsWith(get_site_url().\'\'.$results[$i][\'uri\'], \'http://universitycompare.com/universities/the-uni-of-westminster/\')) {
        echo $results[$i][\'count\'];
        echo \',<br />\';  } ?>
如何将上面的“如果结果”插入到下面的短代码中?

<?php 
echo do_shortcode(\'[wp_charts title="linechart" canvaswidth="976px" canvasheight="244px" relativewidth="4" width="976px" height="244px" type="line" align="alignright" datasets=" { data to be here } "]\'); 
?>

1 个回复
SO网友:Guss

如果我理解正确,您有一个名为wp_charts 接受pramaeterdatasets, 并且您希望创建一个新的短代码来运行数据库查询,并将查询结果嵌入为datasets?

如果是这样的话,有几种方法可以做到这一点,但我认为最简单的方法——尤其是如果你没有写wp_charts 您自己-是创建一个新的快捷码来运行查询,然后调用wp_charts 短代码。

例如:

function my_charts_with_data_shortcode_impl($atts, $content = null) {
    $sql_query = ...
    $results = $wpdb->get_results($sql_query, ARRAY_A );
    $datasets = [];
    foreach ($results as $row) {
        if (endsWith(get_site_url().\'\'.$row[\'uri\'], \'http://universitycompare.com/universities/the-uni-of-westminster/\')) {
            $datasets[] = $row[\'count\'];
        }
    }

    return do_shortcode(\'[wp_charts \'.
        \'title="linechart" canvaswidth="976px" canvasheight="244px" relativewidth="4" \'.
        \'width="976px" height="244px" type="line" align="alignright" \'.
        \'datasets="\'. join(",",$datasets) . \'"]\');
}

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请