首页 文章

单一分类附加到多个帖子类型模板问题

提问于
浏览
1

所以我有一个大约有6种自定义帖子类型的网站 . 这些帖子类型中的三种都附加到一个自定义分类中 .

我希望能够在侧边栏中列出list_cats并吐出像 ../%post_type%/%taxonomy%/ 这样的网址,并将其带到 taxonomy-%taxonomy%.php 模板并仅返回 %post_type% 的结果 .

我可以创建一个条件,它将在 taxonomy-%taxonomy%.php 文件中正确读取当前 %post_type% 和样式 . 我需要一些最有效的方法来修改URL以将 %post_type% 传递给查询 .

提前致谢 . 我搜索了几天没有明确的答案 .

So here are my feeble attempts at a solution yet they are one big FAIL 这是基于rlmseo.com在这里找到答案的讨论 .

Trying to add the post type to a variable to use in the Query:

练习区是一种分类法 . 尝试在taxonomy-practice-areas.php中添加一个可用于过滤分类的变量

function add_query_vars($aVars) {
    $aVars[] = "cust_pt_var";    // represents the name of the custom post type as shown in the URL
    return $aVars;
}

// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');      

function add_rewrite_rules($aRules) {
    $aNewRules = array('practice-areas/([^/]+)/pt/([^/]+)/?$' => 'index.php?practice-areas=$matches[1]&cust_pt_var=$matches[2]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules')

Trying to pass query variables in the rewrite

尝试在重写时将查询类型(组合和客户)以及练习区分类术语添加到查询中 .

function add_rewrite_rules($aRules) {
    $aNewRules = array('portfolio/practice-areas/([^/]+)/?$' => 'index.php?post_type=portfolio&practice-areas=$matches[1]');
    $aNewRules = array('clients/practice-areas/([^/]+)/?$' => 'index.php?post_type=clients&practice-areas=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules')

2 回答

  • 0

    您应该能够通过修改WP的重写规则来实现这一目标:

    add_action( 'init', 'ss_permalinks' );
    function ss_permalinks() {
        add_rewrite_rule(
            'page/remove/([^/]+)/?',
            'index.php?pagename=page&service=$matches[1]',
            'top'
    );
    }
    add_filter( 'query_vars', 'ss_query_vars' );
    function ss_query_vars( $query_vars ) {
        $query_vars[] = 'removeid';
        return $query_vars;
    }
    

    这是不完整的,您需要更改目标URL和规则本身,但这是基础 . 实施后重新保存固定链接设置一次 . page 是当用户访问此URL时指向的页面的slug(在本例中为 domain.com/page/remove/432 ), $matches[1] 应该是URL中 remove/ 之后的数字 . 此编号可通过稍后指定的变量访问,目标页面模板上的 $query_vars[] = 'removeid'; / '$removeid'将是URL中的编号(如果已指定) .

  • 0

    好的,所以我把重写完全放在一起,然后使用$ _POST解决方案 . 它工作得很好,我的URL没有任何污点 .

    Put in Destination Page:

    <?php
        $myvar = $_POST['formVar'];
        query_posts($query_string . '&post_type='.$myvar );
        ?>
    

    Put in Originating Page:

    <!-- The Form: Put this form somewhere on the page: -->
    
        <form method=post name="post_form" action="">
        <input type="hidden" name="formVar" value="">
        </form>
    
    
    <!-- The URL: Here I am assigning the Destination URL to a variable.  This can be whatever you want the destination page to be. -->
    
        <?php                       
        $dest_url = get_bloginfo('url').'/practice-areas/'. $category->slug;
        ?>
    
    
    <!-- The Link: Here we are assigning 'portfolio' to  'formVar' and setting the form 'action' to the $dest_url value. The title stuff is irrelevant here-->
    
        <a href="" onclick="document.post_form.formVar.value='portfolio'; document.post_form.action ='<?php $dest_url; ?>'; document.post_form.submit(); return false" title ="<?php sprintf( __( "View all posts in %s" ), $category->name ); ?>" >
    

相关问题