![](/img/trans.png)
[英]How can i automatically create and save a shortcode for each custom post type when user creates a new custom post type
[英]how to create shortcode using post content for each post when the post is published
每次使用該特定帖子內容發布帖子時,我都想創建短代碼,以便我為每個不同的帖子都有短代碼,為此我編寫了此代碼,但它不起作用,任何人都可以幫忙。
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;
}
如果你只是將一個 id 作為屬性傳遞給這個簡碼怎么辦?
add_shortcode( 'my_banner', 'custom_shortcode' );
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.