1

次のコードを使用してterm_taxonomy_id、URL の末尾から取得した を使用して、ページ テンプレート内からデータベースに対してクエリを実行しています。

このテンプレートhttp://mysite.com/county/18を 18 で使用すると、私の URL は次のようになります。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 構造を変更して、上記の taxonomy_id ではなく、URL の末尾に term_name を使用する必要があります。

だから今私のURLはこのようになりますhttp://mysite.com/county/London

これを行うと、URL を爆発させて最後に taxonomy_id (上記の例では 18) を取得し、その文字列を使用して DB をクエリするため、データベース クエリは機能しません。

実際の質問:用語名からを取得しterm_taxonomy_idて、db クエリで使用できる方法はありますか?

みたいなことを考えている

//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しても何も得られないので、まだ何かが間違っています:(

4

1 に答える 1

1

必要なのは、Wordpress の get_term_by 関数を使用することだと思います。ここで完全なドキュメントを見ることができますhttp://codex.wordpress.org/Function_Reference/get_term_by

最初の 3 つの値が必要なので、get_term_by('slug',explode("/",$_SERVER['REQUEST_URI']),'your_term_category_here'); のようなものが必要になります。

始めるのに役立つことを願っています!

于 2012-11-20T10:43:27.247 に答える