【问题标题】:Disable Wordpress short tag禁用 Wordpress 短标签
【发布时间】:2012-09-04 19:16:40
【问题描述】:

我想修改 Wordpress 短标签的布局。我要修改的标签是将单个帖子分成多个页面的标签。

在我的主题中,我需要禁用该功能并将每个部分包装在一个 div 中。

我知道可以添加过滤器来修改短标签,但显然我做错了。下面的函数似乎没有替换短标签,我仍然得到一个分页列表。

谁能提出一个替换短标签的解决方案?

add_filter( 'the_content', 'reformat_lists' );

function reformat_lists($content){
    $f = '<!--nextpage-->';
    $r = '<div id="cmn-list">';
    str_replace($f,$r,$content);
    return $content;
}

【问题讨论】:

    标签: php string wordpress filter hook


    【解决方案1】:

    很可能您的帖子查询调用了setup_postdata,因此&lt;!--nextpage--&gt; 在您有机会之前已经被替换,因此您可能需要使用另一个过滤器或找出Wordpress 插入的内容。如果您使用get_posts 而不是WP_query,您可以在setup_postdata 之前获得the_content。理想情况下,您可以在 the_content 上找到一个在此之前发生的过滤器,但不是在 DB 写入之前,但我似乎找不到任何工作要做。

    它并不漂亮,因为它是破坏性的(在保存到数据库之前替换标签)而不是在打印之前,但这可能对你有用:

    function reformat_lists($content){
        $f = '<!--nextpage-->';
        $r = '<div id="cmn-list">';
        $content = str_ireplace($f,$r,$content); //don't forget to pass your replaced string to $content
        return $content;
    }
    add_filter( 'content_save_pre', 'reformat_lists');
    

    编辑:更好的是,如果你得到global $post,你可以抓取未过滤的内容。试试下面的 - 我在内容的末尾添加了一个&lt;/div&gt; 以关闭我们插入的内容,这样它就不会破坏您的布局。获取global $post 可能无法在所有情况下都起作用,因此我将在您的设置中进行测试。

    function reformat_lists($content){
        global $post;
        $content = $post->post_content;
        $f = '<!--nextpage-->';
        $r = '<div id="cmn-list">';
        $content = str_ireplace($f,$r,$content) . '</div>';
        return $content;
    }
    add_filter( 'the_content', 'reformat_lists', 1);
    

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2012-06-30
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多