我正在使用以下代码,通过使用从URL末尾检索的term_taxonomy_id
,从页面模板中对数据库进行查询。
当使用这个模板http://mysite.com/county/18
时,我的URL看起来像这样,在这种情况下18是term_taxonomy_id
我的代码如下:
<?php
/*
Template Name: County
*/
$ex = explode("/",$_SERVER['REQUEST_URI']);
//print_r($ex);
get_header();
// Global query variable
global $woo_options;
global $wp_query;
global $wpdb;
$all_terms = $wpdb->get_results("SELECT * FROM wp_term_taxonomy,wp_terms WHERE wp_term_taxonomy.parent='{$ex[2]}' AND wp_term_taxonomy.term_id=wp_terms.term_id ORDER BY wp_terms.name");
//echo "SELECT name FROM wp_terms WHERE term_id='{$ex[2]}'";
$taxName = $wpdb->get_results("SELECT name FROM wp_terms WHERE term_id='{$ex[2]}'");
foreach ( $taxName as $tx ) {
$nameTaxnaomy = $tx->name;
}
$getPostID = $wpdb->get_results("SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='{$ex[2]}'");
foreach ( $getPostID as $tx1 ) {
$post = $tx1->object_id;
}
?>
问题:但是,我需要更改URL结构,以使用URL末尾的term_name,而不是上面提到的taxonomy_id。
所以现在我的URL看起来像这个http://mysite.com/county/London
当我这样做时,数据库查询不起作用,因为它正在分解URL,以获得末尾的taxonomy_id(在上面的示例中为18),然后使用该字符串查询数据库。
实际问题:有没有办法从术语名称中获取term_taxonomy_id
,以便在数据库查询中使用它?
我在想类似的东西
//explode the url to get the term name
$dx = explode("/",$_SERVER['REQUEST_URI']);
//now get the id from the term name
$ex = //get the term_taxonomy_id from $dx
感谢的任何帮助
干杯
更新-通过下面的一些技巧,我将字符串做成了这样。我的分类法名称叫做location
//define $ex byt ge_term_by
$ex = get_term_by('slug',explode("/",$_SERVER['REQUEST_URI']),'location');
当我回显$ex
时,我没有得到任何东西,所以仍然有问题:(
我认为您需要的是使用Wordpress中的get_term_by函数。你可以在这里看到它的完整文档http://codex.wordpress.org/Function_Reference/get_term_by。
前3个值是必需的,因此您需要以下内容get_term_by('slu',爆炸("/",$_SERVER['REQUEST_URI']),'your_term_category_here');
希望这能帮助你开始!