仅在特定页面上运行

时间:2021-02-09 作者:SamuelM

如果我愿意的话,我正在尝试将来只在一个页面或其他页面上运行函数。但我不想在整个网站上运行它。我需要有关如何放置if is\\u页面或if is\\u single的帮助。

// disable auto-formatting
function my_formatter($content) {
    $new_content = \'\';
    $pattern_full = \'{(\\[raw\\].*?\\[/raw\\])}is\';
    $pattern_contents = \'{\\[raw\\](.*?)\\[/raw\\]}is\';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }
    return $new_content;
}
remove_filter(\'the_content\', \'wpautop\');
remove_filter(\'the_content\', \'wptexturize\');
add_filter(\'the_content\', \'my_formatter\', 99);
非常感谢您的帮助。

谢谢

2 个回复
SO网友:Mohammad Zarei

define the function:

// my_formatter - register custom text formatter for specified post or page
function my_formatter($content) {
    $new_content = \'\';
    $pattern_full = \'{(\\[raw\\].*?\\[/raw\\])}is\';
    $pattern_contents = \'{\\[raw\\](.*?)\\[/raw\\]}is\';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }
    return $new_content;
}
只需添加以下代码。你应该用你的desired page ID.

if ( is_page( 100 ) ){ // you should change this line
    remove_filter(\'the_content\', \'wpautop\');
    remove_filter(\'the_content\', \'wptexturize\');
    add_filter(\'the_content\', \'my_formatter\', 99);
}

SO网友:Antti Koskinen

您可能已经解决了这个问题,但这里有一个示例,说明如何有条件地向单个页面或帖子添加过滤器。

// Check, if custom formatter needs to be added
add_action(
    \'template_redirect\',
    function() {
        // don\'t do anything, if not page or post
        if ( ! is_singular( array(\'page\', \'post\') ) ) {
            // some other type
            return;
        }

        // targetted page or post
        // add other post or page slugs or ID to array
        // to use custom formatter on them
        $targets = array(
            \'some-post-name\', // names/slugs
            12, // or IDs
        );

        // check, that current post/page is one of the targets
        global $post;
        $current = array($post->ID, $post->post_name);
        if ( ! array_intersect( $current, $targets ) ) {
            // current ID or slug was not in the targets array
            return;
        }

        // We\'re on a targeted post or page, so let\'s do stuff!

        // remove default formatters
        remove_filter(\'the_content\', \'wpautop\');
        remove_filter(\'the_content\', \'wptexturize\');

        // add custom formatter
        add_filter(\'the_content\', \'my_formatter\', 99);
    }
);

function my_formatter($content) {
    // code...
}

相关推荐

如何在wp-admin的Pages部分下列出新添加的ftp网页?

我通过文件管理器上传了一个自定义页面(html、css和一些脚本),并希望客户能够通过管理区域中的管理“页面”菜单访问此页面。可通过访问该页面www.site.com/{my_page} 但管理区不承认Pages.我应该如何把它放在管理区的页面部分?