【问题标题】:Pass argument to short code function in WordPress将参数传递给 WordPress 中的短代码函数
【发布时间】:2021-08-26 01:56:49
【问题描述】:

我在function.php 中开发了一个短代码函数。我想要做的是将参数传递给该短代码函数,以便它将基于使用该参数的某些查询从数据库中获取数据。我只想知道如何传递论点。就像我在 WordPress 页面上使用了短代码一样。

如何将页面名称或静态文本参数传递给 PHP 中的短代码函数?目前我正在使用静态参数获取数据。

我的短代码功能是:

function db_short() {
    global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM interestclass where Subject='Bakery, Pastory & Cookies' " );
}
add_shortcode( 'show_db_info', 'db_short' );

【问题讨论】:

    标签: php wordpress parameter-passing wordpress-shortcode


    【解决方案1】:
    /* you must change your code to pass agrument in shortcode & fetch same argument in you function code like below */
    
    /* **shortcode** */
    
    [show_db_info name="text here"]
    
    /* **function code** */
     
      
    function db_short($atts) {
        
        $name = ($attr['name']!='' ? $attr['name'] : 'default value here if blank') ;  
        global $wpdb;
    
        $result = $wpdb->get_results ( "SELECT * FROM interestclass where Subject='Bakery, Pastory & Cookies' " );
    
    }
    
       add_shortcode( 'show_db_info', 'db_short' );
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2014-09-26
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    相关资源
    最近更新 更多