在Wordpress中访问functions.php之前的post数据


Accessing post data before functions.php in Wordpress

我正在更新我的自定义Wordpress主题,但由于这是一个相当耗时的过程,我想一次启动一个部分。换句话说,网站的不同部分会有几个版本的主题。为了保持整洁,我想把它们放在单独的文件夹中,包括js、images、css等所有资产。

我设法使用条件标签重写了模板层次结构,但在函数上卡住了。php

我试图使用自定义文件(post-meta)在几个functions.php文件之间切换,但不幸的是$post在那里不可用,所以我无法使用get_post_meta()。

我只能找到一个带有自定义数据库查询、$wpdb等的解决方案,但无法真正找到它。

在加载functions.php之前,有没有相当简单的解决方案可以连接到post数据(wp_query)?或者以某种不同的方式修改函数的加载位置?

为了说明我所写的内容,我粘贴了我的主要index.php

<?
get_header();
/*
 * Get theme version according to the custom field 'section'
 */
 
if( function_exists ( 'theme_version' ) ){
	$theme = theme_version( @get_the_ID() );
} else {
	$theme = 'v2';
}
include_once( 'theme/'. $theme .'/TEMPLATE_BUILDER.php' );
get_footer();
?>

谢谢!

希望找到一个正确的答案(经过几个小时的研究、尝试和错误)

我将以下代码放在main(wp native)functions.php中为了保持代码和文件结构的整洁,工作很有魅力。

add_action('after_setup_theme', function(){
    // parse_url tidies-up the uri
    $section = get_post_meta( url_to_postid( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ),'section', true);
    if ( !empty( $section )){
        // assign sections to the theme versions below
        $theme_version = array(
            'v3' => array(
                'Sixth Form',
                [ ... ]
            ),
            'v3.1' => array(
                'Contact',
                [ ... ]
            )
        );
        foreach($theme_version as $key => $value) { if(in_array( $section, $value )) $theme = $key; }   
    }
    if( empty($theme) ) $theme = 'v2';   // default theme version
    require_once(  'theme/' . $theme . '/functions.php' );
    $GLOBALS['theme-ver'] = $theme; // set the global to use in index.php (and somewhere else perhaps)
});

代码还不完整——需要一些条件子句作为函数。php有时在循环中被调用多次(尤其是使用自定义wp_query)

也许有人会发现以上内容很有用。顺便说一句,WP本身并不支持某种"主题版本控制",这很令人惊讶——我可以看到不必一次将整个网站升级到例如resp的强大好处。