如何重写URL以将分类和POST类型值传递给查询?

时间:2011-01-26 作者:jroakes

所以我有一个网站,大约有6种自定义帖子类型。其中三种帖子类型附加到一个自定义分类法。

我希望能够在侧边栏中列出\\u猫,并弹出一个url,如../%post_type%/%taxonomy%/ 让这个带你去taxonomy-%taxonomy%.php 模板并仅返回%post_type%.

我可以创建一个条件来读取当前%post_type% 并在taxonomy-%taxonomy%.php 文件我需要一些关于修改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:

实践领域是一种分类法。尝试添加一个变量,我可以使用该变量在分类法实践区域中按帖子类型过滤分类法。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\')

1 个回复
SO网友:jroakes

好吧,所以我放弃了重写,并采用了$\\u POST解决方案。它工作得非常好,在我的URL中没有slop。这里的实践领域是分类学,链接拉住了分类学实践领域。php模板文件。在目的地(或模板文件中的此处),我可以修改$query\\u字符串,以进一步将搜索细化到特定的帖子类型。Yeeeee哈!!

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 ); ?>" >

结束

相关推荐