创建快捷代码以显示关系当前日期中的特定帖子

时间:2017-01-31 作者:skifast

您好,我创建了这个脚本,以在每一天和当前关系中看到一位圣人。

我用在模板的标题上,效果很好:

           <?php  
            //Date parameter
            $currentDay = date(\'j\');
            $currentMonth = date(\'n\');
            //wp query to show saints name 
            $con = mysqli_connect("localhost","mydb","mypassword","myuser");
            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $sql = "select * from santi where numero_giorno= ".$currentDay." and numero_mese= ".$currentMonth;
            $result = mysqli_query($con, $sql);
            if (mysqli_affected_rows($con)>0) {
                while($row = $result->fetch_assoc()){
        ?>
        <span id="txtSaintName"><?php echo  $row[\'nome\']?></span>
        <?php
                }
                mysqli_close($con);
            }
        ?>
现在,我需要将其转换为短代码,以便在小部件中使用。

我尝试这样做,但如果我复制页面或小部件上的短代码,并试图看到我只使用当天的数字,而不是文本。哪里错了?

<?php 

add_shortcode("separatore", "separatore_html_render");  

function separatore_html_render( $atts ) {  
return                <?php  
                //Date parameter
                $currentDay = date(\'j\');
                $currentMonth = date(\'n\');
                //wp query to show saints name 
                $con = mysqli_connect("localhost","mydb","mypassword","myuser");
                if (mysqli_connect_errno()) {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
                $sql = "select * from santi where numero_giorno= ".$currentDay." and numero_mese= ".$currentMonth;
                $result = mysqli_query($con, $sql);
                if (mysqli_affected_rows($con)>0) {
                    while($row = $result->fetch_assoc()){
            ?>
            <span id="txtSaintName"><?php echo  $row[\'nome\']?></span>
            <?php
                    }
                    mysqli_close($con);
                }
            ?>;  
}  

1 个回复
SO网友:Arsalan Mithani

尝试使用所需名称作为函数名称:

function main_func_name() {

    //Date parameter
    $currentDay = date(\'j\');
    $currentMonth = date(\'n\');
    //wp query to show saints name 
    $con = mysqli_connect("localhost", "mydb", "mypassword", "myuser");
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql = "select * from santi where numero_giorno= " . $currentDay . " and numero_mese= " . $currentMonth;
    $result = mysqli_query($con, $sql);
    if (mysqli_affected_rows($con) > 0) {
        while ($row = $result->fetch_assoc()) {
            ?>
                         <span id="txtSaintName"><?php echo $row[\'nome\'] ?></span>
            <?php
        }
        mysqli_close($con);
    }
}

add_shortcode(\'shortcode_name\', \'function_shortcode\');

function function_shortcode() {
    ob_start();
    main_func_name();
    return ob_get_clean();
}
?>

相关推荐

Namespaced shortcode?

我正在改造一个旧的WP站点,该站点有许多自定义的短代码,显然由于代码当前的组织方式,这些短代码在性能方面付出了代价。当然,我可以修复优化不好的代码,使用十几个短代码,并且一天就可以完成,但我想知道如何更好地组织它们。根据WordPress\'documentation, 建议将它们放在插件中并在上初始化init. 我们可以通过这样“命名”它们来减少这个钩子中的负载吗?[com.company shortcode attr=\"attr\" prop=\"prop\"] 有人尝试过这样的解决方案吗