是否建议使用`add_rewrite_Rule()`为自定义页面提供服务?

时间:2014-01-27 作者:WolfieZero

我有一个名为“动态”的页面,它根本没有存储在post表中(不是post、页面或任何类型的自定义post类型);原因是所有数据都是基于$_GET 请求。

这是我如何做到这一点的一个非常接近的表现

add_action( \'init\', array( $this, \'add_custom_rewrite\' ) );
add_action( \'parse_request\', array( $this, \'custom_requests\' ) );
add_filter( \'query_vars\', array( $this, \'custom_query_vars\' ) );

function add_custom_rewrite() {
    add_rewrite_rule(
        \'dynamic/?$\',
        \'index.php?dynamic=true\',
        \'top\'
    );
}

function custom_requests( $wp ) {
    if( !empty( $wp->query_vars[\'dynamic\'] ) ) {
        dynamic( $_GET );
    }
}

function dynamic( $get ) {
    // ... do stuff with $get
    load_template( get_template_directory() . \'/dynamic.php\' );
    exit;
}
现在我问这个问题,因为这一页似乎完成得很早。它加载wp_footer() 功能和所有好东西,但不像其他所有标准页面那样显示管理栏。

有没有更好的方法来解决这个问题;可能使用数据库中存储的真实post类型并引用它?

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

尝试改用template_redirect

这里我有一篇关于使用这种方法的博客:

http://www.tomjn.com/386/content-without-posts/

我对使用此方法的管理工具栏没有任何问题。

它包括一个助手类:

<?php

/**
 * A helper class to implement arbitrary content at arbitrary URLs without a supporting post or page.
 * Inherit from this class and implement the render_page method
 * 
 * @author: Tom J Nowell ww.tomjn.com
 * @License: GPL 2+
 */
abstract class Tomjn_Custom_Page {

    /**
     * Saves the options container and sets up some WP hooks
     */
    public function __construct( $options ) {
        $this->options = $options;

        // add our rewrite rules
        add_filter( \'generate_rewrite_rules\', array( $this, \'custom_page_generate_rewrite_rules\' ) );
        // add our custom query variable to the whitelist
        add_filter( \'query_vars\', array( $this, \'custom_page_query_vars\' ) );
        // dont pull in a full listing of posts in the main query, there\'s no need
        // ( you can comment this out if you\'re not using a theme template to render content )
        add_filter( \'pre_get_posts\', array( $this, \'custom_page_paging_issue\' ) );
        // call render_page() when needed
        add_action( \'template_redirect\', array( $this, \'custom_page_template_redirect\' ) );
    }

    /**
     * Add our rewrite rules
     */
    function custom_page_generate_rewrite_rules( $wp_rewrite ) {
        $pagename = $this->options[\'pagename\'];
        $custom_page_rules = array(
            $this->options[\'url\'] => \'index.php?custom_page=\'.$pagename.\'&posts_per_page=1&paged=1\'
        );
        $wp_rewrite->rules = $custom_page_rules + $wp_rewrite->rules;
    }

    /**
     * Filter that inserts our query variable into the $wp_query
     */
    function custom_page_query_vars( $qvars ) {
        $qvars[] = \'custom_page\';
        return $qvars;
    }

    /**
     * fix page loops if pulling in a theme template
     */
    function custom_page_paging_issue( $query ) {
        if ( !empty( $query->query_vars[\'custom_page\'] ) ) {
            $query->set( \'posts_per_page\', 1 );
        }
    }

    /**
     * Filter that maps the query variable to a template
     */
    function custom_page_template_redirect() {
        $pagename = $this->options[\'pagename\'];
        global $wp_query;
        $custom_page = $wp_query->query_vars[\'custom_page\'];
        if ( $custom_page == $pagename ) {
            // we\'ve found our page, call render_page and exit
            $this->render_page();
            exit;
        }
    }

    /**
     * Displays the content, extend this class and implement this function as needed
     */
    public abstract function render_page();

}
使用它非常简单:

require_once( \'custom_page.php\' );

class Hello_World_Page extends Tomjn_Custom_Page {
    public function render_page() {
        echo \'hello world!\';
    }
}

$helloworld = new Hello_World_Page( array(
    \'url\'       => \'helloworld\',
    \'pagename\'  => \'helloworld\'
));

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register