本文整理汇总了PHP中category_exists函数的典型用法代码示例。如果您正苦于以下问题:PHP category_exists函数的具体用法?PHP category_exists怎么用?PHP category_exists使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了category_exists函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: wp_insert_category
function wp_insert_category($catarr)
{
global $wpdb;
extract($catarr, EXTR_SKIP);
if (trim($cat_name) == '') {
return 0;
}
$cat_ID = (int) $cat_ID;
// Are we updating or creating?
if (!empty($cat_ID)) {
$update = true;
} else {
$update = false;
}
$name = $cat_name;
$description = $category_description;
$slug = $category_nicename;
$parent = $category_parent;
$parent = (int) $parent;
if (empty($parent) || !category_exists($parent) || $cat_ID && cat_is_ancestor_of($cat_ID, $parent)) {
$parent = 0;
}
$args = compact('name', 'slug', 'parent', 'description');
if ($update) {
$cat_ID = wp_update_term($cat_ID, 'category', $args);
} else {
$cat_ID = wp_insert_term($cat_name, 'category', $args);
}
if (is_wp_error($cat_ID)) {
return 0;
}
return $cat_ID['term_id'];
}
示例2: test_category_exists_should_respect_nonempty_parent
/**
* @ticket 30975
*/
public function test_category_exists_should_respect_nonempty_parent()
{
$c1 = $this->factory->category->create();
$c2 = $this->factory->category->create(array('name' => 'Foo', 'parent' => $c1));
$c3 = $this->factory->category->create(array('name' => 'Foo'));
$found = category_exists('Foo', $c1);
$this->assertEquals($found, $c2);
}
示例3: _category_exists
function _category_exists($cat_id)
{
global $wpdb;
$cat_id = (int) $cat_id;
$maybe_exists = category_exists($cat_id);
if ($maybe_exists) {
return true;
} else {
return false;
}
}
示例4: ConvertSubject
function ConvertSubject()
{
global $wpdb;
$subjects = $wpdb->get_results("SELECT * FROM subject", 'ARRAY_A');
/*
foreach ($subjects as $subject) {
$tag_id = '';
$tag_id = term_exists($subject['name']);
if ($tag_id == 0){
$tag_id = wp_create_term($subject['name'],'post_tag' );
$tag_id = $wpdb->get_var("SELECT MAX(`term_id`) FROM $wpdb->terms");
$wpdb->insert(
'subject_to_tag',
array( 'subject_id' => $subject['id'], 'tag_id' => $tag_id ),
array( '%d', '%d' )
);
}
}
*/
foreach ($subjects as $category) {
if ($term_id = category_exists($category['name'])) {
$wpdb->insert('category_to_terms', array('category_id' => $category['id'], 'term_id' => $term_id), array('%d', '%d'));
} else {
$parent = $wpdb->get_var("SELECT `term_id` FROM `category_to_terms` WHERE `category_id` = '" . $category['parent'] . "'");
if ($term_id = wp_create_category($category['name'], $parent)) {
$wpdb->insert('category_to_terms', array('category_id' => $category['id'], 'term_id' => $term_id), array('%d', '%d'));
}
}
}
$count = $wpdb->get_var("SELECT COUNT(*) FROM `subject_to_tag`");
?>
<html>
<head>
<meta charset='UTF-8' />
</head>
<body>
<?php
echo $count;
?>
</body>
</html>
<!-- end HTML part -->
<?php
SelestAction();
}
示例5: wp_create_categories
/**
* Create categories for the given post.
*
* @since 2.0.0
*
* @param array $categories List of categories to create.
* @param int $post_id Optional. The post ID. Default empty.
* @return List of categories to create for the given post.
*/
function wp_create_categories($categories, $post_id = '')
{
$cat_ids = array();
foreach ($categories as $category) {
if ($id = category_exists($category)) {
$cat_ids[] = $id;
} elseif ($id = wp_create_category($category)) {
$cat_ids[] = $id;
}
}
if ($post_id) {
wp_set_post_categories($post_id, $cat_ids);
}
return $cat_ids;
}
示例6: wp_insert_category
function wp_insert_category($catarr, $wp_error = false) {
$cat_defaults = array('cat_ID' => 0, 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
$cat_arr = wp_parse_args($cat_arr, $cat_defaults);
extract($catarr, EXTR_SKIP);
if ( trim( $cat_name ) == '' ) {
if ( ! $wp_error )
return 0;
else
return new WP_Error( 'cat_name', __('You did not enter a category name.') );
}
$cat_ID = (int) $cat_ID;
// Are we updating or creating?
if ( !empty ($cat_ID) )
$update = true;
else
$update = false;
$name = $cat_name;
$description = $category_description;
$slug = $category_nicename;
$parent = $category_parent;
$parent = (int) $parent;
if ( $parent < 0 )
$parent = 0;
if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
$parent = 0;
$args = compact('name', 'slug', 'parent', 'description');
if ( $update )
$cat_ID = wp_update_term($cat_ID, 'category', $args);
else
$cat_ID = wp_insert_term($cat_name, 'category', $args);
if ( is_wp_error($cat_ID) ) {
if ( $wp_error )
return $cat_ID;
else
return 0;
}
return $cat_ID['term_id'];
}
示例7: wpinstaroll_getInstagramGeneratedDraftPosts
function wpinstaroll_getInstagramGeneratedDraftPosts()
{
$category_for_post = get_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_category');
if (empty($category_for_post)) {
$category_for_post = 'Uncategorized';
update_option(WP_ROLL_INSTAGRAM_PLUGIN_PREFIX . '_instagram_post_category', $category_for_post);
}
$cat_id = category_exists($category_for_post);
if ($cat_id) {
$category = get_category($cat_id);
$category_slug = $category->slug;
return get_bloginfo('wpurl') . '/wp-admin/edit.php?post_status=draft&post_type=post&category_name=' . $category_slug;
} else {
return get_bloginfo('wpurl') . '/wp-admin/edit.php?post_status=draft&post_type=post';
}
}
示例8: process_categories
function process_categories()
{
global $wpdb;
$cat_names = (array) $wpdb->get_col("SELECT cat_name FROM {$wpdb->categories}");
while ($c = array_shift($this->categories)) {
$cat_name = trim(str_replace(array('<![CDATA[', ']]>'), '', $this->get_tag($c, 'wp:cat_name')));
// If the category exists we leave it alone
if (in_array($cat_name, $cat_names)) {
continue;
}
$category_nicename = $this->get_tag($c, 'wp:category_nicename');
$posts_private = (int) $this->get_tag($c, 'wp:posts_private');
$links_private = (int) $this->get_tag($c, 'wp:links_private');
$parent = $this->get_tag($c, 'wp:category_parent');
if (empty($parent)) {
$category_parent = '0';
} else {
$category_parent = category_exists($parent);
}
$catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
$cat_ID = wp_insert_category($catarr);
}
}
示例9: process_categories
function process_categories()
{
global $wpdb;
$cat_names = (array) get_terms('category', 'fields=names');
while ($c = array_shift($this->categories)) {
$cat_name = trim($this->get_tag($c, 'wp:cat_name'));
// If the category exists we leave it alone
if (in_array($cat_name, $cat_names)) {
continue;
}
$category_nicename = $this->get_tag($c, 'wp:category_nicename');
$category_description = $this->get_tag($c, 'wp:category_description');
$posts_private = (int) $this->get_tag($c, 'wp:posts_private');
$links_private = (int) $this->get_tag($c, 'wp:links_private');
$parent = $this->get_tag($c, 'wp:category_parent');
if (empty($parent)) {
$category_parent = '0';
} else {
$category_parent = category_exists($parent);
}
$catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name', 'category_description');
$cat_ID = wp_insert_category($catarr);
}
}
示例10: test_wp_insert_delete_category
function test_wp_insert_delete_category() {
$term = rand_str();
$this->assertNull( category_exists( $term ) );
$initial_count = wp_count_terms( 'category' );
$t = wp_insert_category( array( 'cat_name' => $term ) );
$this->assertTrue( is_numeric($t) );
$this->assertFalse( is_wp_error($t) );
$this->assertTrue( $t > 0 );
$this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) );
// make sure the term exists
$this->assertTrue( term_exists($term) > 0 );
$this->assertTrue( term_exists($t) > 0 );
// now delete it
$this->assertTrue( wp_delete_category($t) );
$this->assertNull( term_exists($term) );
$this->assertNull( term_exists($t) );
$this->assertEquals( $initial_count, wp_count_terms('category') );
}
示例11: test_query_cat
function test_query_cat()
{
$cat = category_exists('cat-b');
$posts = $this->q->query("cat={$cat}");
// there are 4 posts with Cat B
$this->assertCount(4, $posts);
$this->assertEquals('cat-b', $posts[0]->post_name);
$this->assertEquals('cats-b-and-c', $posts[1]->post_name);
$this->assertEquals('cats-a-and-b', $posts[2]->post_name);
$this->assertEquals('cats-a-b-c', $posts[3]->post_name);
}
示例12: cat2wp
function cat2wp($categories = '')
{
// General Housekeeping
global $wpdb;
$count = 0;
$dccat2wpcat = array();
// Do the Magic
if (is_array($categories)) {
echo '<p>' . __('Importing Categories...') . '<br /><br /></p>';
foreach ($categories as $category) {
$count++;
extract($category);
// Make Nice Variables
$name = $wpdb->escape($cat_libelle_url);
$title = $wpdb->escape(csc($cat_libelle));
$desc = $wpdb->escape(csc($cat_desc));
if ($cinfo = category_exists($name)) {
$ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
} else {
$ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title, 'category_description' => $desc));
}
$dccat2wpcat[$id] = $ret_id;
}
// Store category translation for future use
add_option('dccat2wpcat', $dccat2wpcat);
echo '<p>' . sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count) . '<br /><br /></p>';
return true;
}
echo __('No Categories to Import!');
return false;
}
示例13: wp_specialchars
$cat_name = wp_specialchars(stripslashes($cat_name));
$x->add(array('what' => 'link-category', 'id' => $cat_id, 'data' => "<li id='link-category-{$cat_id}'><label for='in-link-category-{$cat_id}' class='selectit'><input value='{$cat_id}' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-{$cat_id}'/> {$cat_name}</label></li>", 'position' => -1));
}
$x->send();
break;
case 'add-cat':
// From Manage->Categories
check_ajax_referer('add-category');
if (!current_user_can('manage_categories')) {
die('-1');
}
if ('' === trim($_POST['cat_name'])) {
$x = new WP_Ajax_Response(array('what' => 'cat', 'id' => new WP_Error('cat_name', __('You did not enter a category name.'))));
$x->send();
}
if (category_exists(trim($_POST['cat_name']))) {
$x = new WP_Ajax_Response(array('what' => 'cat', 'id' => new WP_Error('cat_exists', __('The category you are trying to create already exists.'), array('form-field' => 'cat_name'))));
$x->send();
}
$cat = wp_insert_category($_POST, true);
if (is_wp_error($cat)) {
$x = new WP_Ajax_Response(array('what' => 'cat', 'id' => $cat));
$x->send();
}
if (!$cat || !($cat = get_category($cat))) {
die('0');
}
$level = 0;
$cat_full_name = $cat->name;
$_cat = $cat;
while ($_cat->parent) {
示例14: process_categories
function process_categories()
{
global $wpdb;
$cat_names = (array) get_terms('category', array('fields' => 'names'));
while ($c = array_shift($this->categories)) {
$cat_name = trim($this->get_tag($c, 'wp:cat_name'));
// If the category exists we leave it alone
if (in_array($cat_name, $cat_names)) {
continue;
}
$category_nicename = $this->get_tag($c, 'wp:category_nicename');
$category_description = $this->get_tag($c, 'wp:category_description');
$posts_private = (int) $this->get_tag($c, 'wp:posts_private');
$links_private = (int) $this->get_tag($c, 'wp:links_private');
$parent = $this->get_tag($c, 'wp:category_parent');
if (empty($parent)) {
$category_parent = '0';
} else {
$category_parent = category_exists($parent);
}
$catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name', 'category_description');
print '<em>' . sprintf(__('Importing category <em>%s</em>…', 'wordpress-importer'), esc_html($cat_name)) . '</em><br />' . "\n";
$cat_ID = wp_insert_term($cat_name, 'category', $catarr);
}
}
示例15: new_post
function new_post()
{
global $user_ID;
if (empty($_POST['action']) || $_POST['action'] != 'new_post') {
die('-1');
}
if (!is_user_logged_in()) {
die('<p>' . __('Error: not logged in.', 'p2') . '</p>');
}
if (!(current_user_can('publish_posts') || get_option('p2_allow_users_publish') && $user_ID)) {
die('<p>' . __('Error: not allowed to post.', 'p2') . '</p>');
}
check_ajax_referer('ajaxnonce', '_ajax_post');
$user = wp_get_current_user();
$user_id = $user->ID;
$post_content = $_POST['posttext'];
$tags = trim($_POST['tags']);
$title = $_POST['post_title'];
$post_type = isset($_POST['post_type']) ? $_POST['post_type'] : 'post';
// Strip placeholder text for tags
if (__('Tag it', 'p2') == $tags) {
$tags = '';
}
if (empty($title) || __('Post Title', 'p2') == $title) {
// For empty or placeholder text, create a nice title based on content
$post_title = p2_title_from_content($post_content);
} else {
$post_title = $title;
}
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
require_once ABSPATH . WPINC . '/category.php';
$accepted_post_cats = apply_filters('p2_accepted_post_cats', array('post', 'quote', 'status', 'link'));
$post_cat = in_array($_POST['post_cat'], $accepted_post_cats) ? $_POST['post_cat'] : 'status';
if (!category_exists($post_cat)) {
wp_insert_category(array('cat_name' => $post_cat));
}
$post_cat = get_category_by_slug($post_cat);
/* Add the quote citation to the content if it exists */
if (!empty($_POST['post_citation']) && 'quote' == $post_cat->slug) {
$post_content = '<p>' . $post_content . '</p><cite>' . $_POST['post_citation'] . '</cite>';
}
$post_content = p2_list_creator($post_content);
$post_id = wp_insert_post(array('post_author' => $user_id, 'post_title' => $post_title, 'post_content' => $post_content, 'post_type' => $post_type, 'post_category' => array($post_cat->cat_ID), 'tags_input' => $tags, 'post_status' => 'publish'));
echo $post_id ? $post_id : '0';
}