【问题标题】:If has_term, load other Wordpress theme如果 has_term,加载其他 Wordpress 主题
【发布时间】:2014-01-07 11:02:30
【问题描述】:

这就是我想要完成的。在 Wordpress 中,我创建了一个名为 categorie 的分类法,其中包含术语 appwebbranding。当一个项目有术语应用程序时,我想加载另一个主题/博客。当一个项目有网络或品牌这个词时,我想加载single.php。最后一个效果很好。

这是我目前的代码

function load_single_template($template) {
$new_template = '';

if( is_single() ) {
    global $post;

    if( has_term('app', 'categorie', $post) ) {
        $new_template = get_theme_roots('themeApp');
    } else {
        $new_template = locate_template(array('single.php' ));
    }
}
return ('' != $new_template) ? $new_template : $template;

}
add_action('template_include', 'load_single_template');

所以当一个项目有app这个词时,我想加载主题themeApp。有什么建议么?提前致谢。

【问题讨论】:

  • 我找到了解决方法。我使用了基于 Head2Space SEO 插件的gdHeadSpace 插件。使用该插件,我可以在我的自定义帖子中引用另一个主题。

标签: php wordpress if-statement taxonomy term


【解决方案1】:

我们必须在我们的插件 AppPresser 中完成类似的任务。您可以在这里查看我们的解决方案:https://github.com/WebDevStudios/AppPresser/blob/master/inc/theme-switcher.php

基本上,您需要在 3 个过滤器中更改主题名称:'template'、'option_template'、'option_stylesheet'。

但获取类别并不是那么简单,因为模板检查在 WordPress 进程中发生得足够早,以至于全局 $post$wp_query 对象不可用。

这是一种可以实现的方法:

<?php
add_action( 'setup_theme', 'maybe_theme_switch', 10000 );
function maybe_theme_switch() {

    // Not on admin
    if ( is_admin() )
        return;

    $taxonomy = 'category';
    $term_slug_to_check = 'uncategorized';
    $post_type = 'post';

    // This is one way to check if we're on a category archive page
    if ( false !== stripos( $_SERVER['REQUEST_URI'], $taxonomy ) ) {
        // Remove the taxonomy and directory slashes and it SHOULD leave us with just the term slug
        $term_slug = str_ireplace( array( '/', $taxonomy ), '', $_SERVER['REQUEST_URI'] );

        // If the term slug matches the one we're checking, do our switch
        if ( $term_slug == $term_slug_to_check ) {
            return yes_do_theme_switch();
        }
    }

    // Try to get post slug from the URL since the global $post object isn't available this early
    $post = get_page_by_path( $_SERVER['REQUEST_URI'], OBJECT, $post_type );
    if ( ! $post )
        return;

    // Get the post's categories
    $cats = get_the_terms( $post, $taxonomy );
    if ( ! $cats )
        return;

    // filter out just the category slugs
    $term_slugs = wp_list_pluck( $cats, 'slug' );
    if ( ! $term_slugs )
        return;

    // Check if our category to check is there
    $is_app_category = in_array( $term_slug_to_check, $term_slugs );
    if ( ! $is_app_category )
        return;

    yes_do_theme_switch();

}

function yes_do_theme_switch( $template ) {
    // Ok, switch the current theme.
    add_filter( 'template', 'switch_to_my_app_theme' );
    add_filter( 'option_template', 'switch_to_my_app_theme' );
    add_filter( 'option_stylesheet', 'switch_to_my_app_theme' );
}

function switch_to_my_app_theme( $template ) {
    // Your theme slug
    $template = 'your-app-theme';
    return $template;
}

【讨论】:

  • 非常感谢Justin Sternberg 的信息。如上所述,我现在正在使用gdHeadSpace 插件,这是一种相当简单的方法来做我想做的事。但我一定会查看信息以获得更理想的解决方案。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
相关资源
最近更新 更多