每次使用該特定帖子內容發布帖子時,我都想創建短代碼,以便我為每個不同的帖子都有短代碼,為此我撰寫了此代碼,但它不起作用,任何人都可以幫忙。
add_action('publish_adsmanager_banner','create_banner_shortcode');
function create_banner_shortcode()
{
add_shortcode( 'banner_'.get_the_ID().'', 'custom_shortcode' );
}
function custom_shortcode($post_id) {
$message = get_the_content($post_id);
return $message;
}
uj5u.com熱心網友回復:
如果你只是將一個 id 作為屬性傳遞給這個簡碼怎么辦?
add_shortcode( 'my_banner', 'custom_shortcode' ); // [my_banner id="123"]
function custom_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => null,
),
$atts
);
$message = get_the_content( $atts['id'] );
return $message;
}
在您最初的想法中,您不能只創建一次以 id 命名的短代碼,它應該在每個 wp init 上注冊。但是在這種情況下,很難僅從短代碼名稱中獲取帖子背景關系,您必須在每個短代碼注冊時傳遞一個 id。上面的解決方案將允許您保持 DRY,特別是如果您在使用它們時知道每個橫幅的 ID。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418954.html
標籤:
上一篇:在優化查詢方面需要幫助
