douling1936 2017-07-21 11:53
浏览 73
已采纳

如何在Wordpress HTML中删除短代码

I have a custom theme where when I upload an image with caption, the caption shortcode is shown on the page: [caption id="attachment_109"].... Using either of WP's default themes, I see no issue. On inspection, WP uses the_content(), link, so I need to do some stripping. I get my posts with get_page_by_path():

<?php

$post = get_page_by_path( $current_page->post_name, OBJECT, 'service' );

// I assume this would work
$content = the_content($post->post_content);

//Blank page:
echo $content;

Echoing $post->post_content shows the caption shortcode as mentioned above. How to get rid of if it? BTW, I need the caption values.

  • 写回答

1条回答 默认 最新

  • donglie7268 2017-07-21 12:04
    关注

    You can get the post content like this

    $post = get_post(123);  //pass the id of the post
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    echo $content;
    

    or

    $content=apply_filters('the_content', get_post_field('post_content', 123)); //123 is the post id
    

    after that just strip the shortcode also you can check if the post is having the shortcode in it or not by has_shortcode

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?