簡體   English   中英

將php文件插入Wordpress短代碼

[英]Inserting a php file into a Wordpress shortcode

我已經制作了一個海關腳本來輸出特定的電話號碼,具體取決於用戶來自何處。 我已經能夠使用以下簡單代碼段在網站的標題中進行設置:

<?php include 'TelephoneNumber.php';?>

現在,我需要創建一個簡碼,以便可以在Wordpress測試編輯器中調用它。 我創建了一個非常簡單的簡碼,但無法計算出上面的文件名。

custom_shortcodes.php(放在wp-content文件夾中)

<?php
function telephone_number(){
return 'PHONE NUMBER HERE';
} // End telephone_number()
?>

function.php(主題)

include(WP_CONTENT_DIR . '/custom_shortcodes.php');
add_shortcode( 'telephone_number_sc', 'telephone_number' );

在這時,短代碼確實會在此處輸出文本PHONE NUMBER。 但是,這沒有用,因為它沒有調用我需要的腳本。

如何使用包含文件功能?

您可以將功能與簡碼放在同一文件中,也可以將它們放在單獨的文件中,但是它們的結構必須正確。 包含作品的方式與要求相同,我只是更喜歡要求。

在同一文件中

在functions.php中

您需要要求/包括簡碼文件:

require('custom_shortcodes.php');

在custom_shortcodes.php中

// phone number function
function telephone_number(){
        return 'PHONE NUMBER HERE';
} // End telephone_number()

要編寫簡碼,您需要遵循以下特定方式:

//shortcode
function output_shorcode($atts, $content = null){
     $number = telephone_number();
     return $number;
}
add_shortcode( 'number', 'output_shortcode' );

並把[數字]放在需要的地方

在單獨的文件中

在functions.php中

您需要要求/包括簡碼文件和數字文件:

require('phone_number.php');
require('custom_shortcodes.php');

在custom_shortcodes.php中

function output_shorcode($atts, $content = null){
         $number = telephone_number();
         return $number;
    }
    add_shortcode( 'number', 'output_shortcode' );

在phone_number.php中

// phone number function
    function telephone_number(){
            return 'PHONE NUMBER HERE';
    } // End telephone_number()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM