【问题标题】:PHP Know if category exist in WordpressPHP 知道 Wordpress 中是否存在类别
【发布时间】:2012-07-03 14:09:59
【问题描述】:

我正在编写一个简单的脚本 php。 此脚本必须知道 wordpress cms 中是否存在通过 $_get 传递的指定类别。

exist.php?cat=xxx

我已经阅读了这篇文章,但我不确定它是否对我有帮助。 http://codex.wordpress.org/Function_Reference/is_category

谁能帮助我? 我能怎么做? 谢谢。

【问题讨论】:

标签: php wordpress list add categories


【解决方案1】:

因为看起来您正试图从一个单独的 PHP 文件中访问它,所以在您的脚本中连接到 WordPress 数据库并运行此查询:

<?php
include 'wp-config.php';

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);

$stmt = $dbh->prepare( "SELECT * FROM `wp_term_taxonomy` TT RIGHT JOIN `wp_terms` TE ON TT.`term_id` = TE.`term_id` WHERE TT.`taxonomy` = 'category' AND TE.`name` = :category" );
$stmt->bindParam( ':category', $_GET['cat'] );
$stmt->execute();

if ( $row = $stmt->fetch() ) {
    // It exists
} else {
    // It does not exist
}

【讨论】:

    【解决方案2】:

    使用get_categories 获取一个数组中的所有类别,然后使用in_array 查找该类别是否存在。然后,您也可以使用相同的类别 ID 来获取有关该类别的更多信息。 参考:http://codex.wordpress.org/Function_Reference/get_categories#Source_File 参考:http://php.net/manual/en/function.in-array.php

    例子:

    <?php 
    $cat_list = get_categories(); 
    
    if (in_array($_GET['cat'], $cat_list)) {
    // exists
    } else {
    // does not exists
    }    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2011-02-06
      • 2018-01-06
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多