WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trying to retrieve category slug with PHP (3 posts)

  1. amenitya
    Member
    Posted 5 years ago #

    I've searched through the forums and tried every combination suggested for retrieving the slug for my categories:

    $categories = $wpdb->get_col($cat_query);
    		foreach ($categories as $category) {
    			echo '<category>'.$category.'</category>'.
    			//add in slug
    				 '<slug>'.$category->slug.'</slug>';
    		}

    ...the category itself comes through fine, but the slug (I've checked a million times that its been assigned a slug of course) doesn't appear:

    <?xml version="1.0" encoding="UTF-8"?>
    <channel><title>GeoMashup Query</title><item><category>alcachofa de Benicarló</category><slug></slug

    It's probably a stupid syntax error, but I'd really appreciate it if someone could point it out to me.

  2. MichaelH
    Member
    Posted 5 years ago #

    On a 2.6.3 test install, this listed all category slugs where the category had at least one post:

    <?php
    $categories = get_categories();
    foreach($categories as $category){
    if($category->count > 0){
    echo $category->slug;
    }
    }
    ?>
  3. amenitya
    Member
    Posted 5 years ago #

    Thanks so much.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags