0

I am a loss here since I have looked at dozens of tutorials and posts all over the internet on how to get the slug for a taxonomy and nothing I try seems to work. The taxonomy shows up fine when I want to edit the post and everything seems to be working fine, however I cannot seem for the life of me to get Wordpress to give me the slugs I want. I am really frustrated and irritated on how complicated the process of saying "for the array of posts you already have, include the taxonomy for each one". sigh

In reality, all I want is to get the custom taxonomy slug that has been selected for each post. I have a portfolio page that I want to make interactive with jquery and I am trying to use the taxonomy slug as the anchors for the jquery script. All I need from Wordpress is to see the post it's already giving me all of it's information and to include the taxonomy it has been already linked to. Then echo that slug into the class of the LI tag.

Here is the code:

<ul>
    <?php
    $post = 'project'; // custom post type
    $i=1;
    query_posts("post_type=$post&showposts=-1&order=asc");
    $post_count = wp_count_posts();
    while(have_posts()) : the_post();
    $myExcerpt = get_the_excerpt();
    $tags = array("<p>", "</p>");
    $myExcerpt = str_replace($tags, "", $myExcerpt);
    $terms = get_the_terms( $post->id, 'project-type' ); // get an array of all the terms as objects.

    $terms_slugs = array();

    foreach( $terms as $term ) {
        $terms_slugs[] = $term->slug; // save the slugs in an array
    }
?>
    <li class="project <?php echo $terms_slugs[$i]; ?>">
        <a href="<?php the_permalink(); ?>" id="project_<?php echo $id; ?>" class="project_frame">
            <?php the_post_thumbnail('full', array('class' => 'project_img')); ?>
            <p style="display: none;"><span class="larger"><?php the_title(); ?></span><br /><span class="smaller"><?php echo $myExcerpt; ?></span></p>
        </a>
    </li>
    <?php 
        $i++;
    endwhile;
    wp_reset_query();
?>
</ul>

Hopefully someone can help me with this.

UPDATE:

Figured it out. Here is the corrected code.

<ul>
                    <?php
                        $post = 'project'; // custom post type
                        $i=0;
                        $counter = 0;
                        query_posts("post_type=$post&showposts=-1&order=asc");
                        $post_count = wp_count_posts();
                        while(have_posts()) : the_post();
                        $myExcerpt = get_the_excerpt();
                        $tags = array("<p>", "</p>");
                        $myExcerpt = str_replace($tags, "", $myExcerpt);

                        $slugs = get_terms('project-type');
                        foreach($slugs as $slug) {
                            $slug_array[$i] = $slug->slug;
                            $i++;
                        }
                    ?>
                    <li class="project <?php echo $slug_array[$counter]; ?>">
                        <a href="<?php the_permalink(); ?>" id="project_<?php echo $id; ?>" class="project_frame">
                            <?php the_post_thumbnail('full', array('class' => 'project_img')); ?>
                            <p style="display: none;"><span class="larger"><?php the_title(); echo $i;?></span><br /><span class="smaller"><?php echo $myExcerpt; ?></span></p>
                        </a>
                    </li>
                    <?php
                        $counter++;
                        endwhile;
                        wp_reset_query();
                    ?>
                </ul>
4
  • 1
    Please write your answer as an answer, and mark the question as answered then.
    – fuxia
    Commented Sep 27, 2012 at 4:53
  • Won't let me. It the site said I need at least 10 points and I only have one.
    – rgaspary
    Commented Sep 27, 2012 at 14:44
  • You can answer your own question … 8 hours after you asked.
    – fuxia
    Commented Sep 27, 2012 at 15:08
  • Any word on making this an answer? Commented Oct 7, 2012 at 1:54

2 Answers 2

1

I would not use query_posts and instead use post_class and a new WP_Query object. to insert the taxonomy slugs. This is more "future proof" and you can put it in a plugin to keep the same functionality across themes.

This works the same exact way as what you're doing (get_the_terms, etc), but it keeps a lot of that cruft out of your template.

<?php
class Taxonomy_Post_Class
{
    /**
     * The post type to which you want to add classes. CHANGE THIS.
     *
     */
    const TYPE = 'project';

    /**
     * the taxonomy whose slugs you want to add. CHANGE THIS.
     *
     */
    const TAX = 'project-type';

    private static $ins = null;

    public static function instance()
    {
        is_null(self::$ins) && self::$ins = new self;
        return self::$ins;
    }

    public static function init()
    {
        add_filter('post_class', array(self::instance(), 'add_class'), 10, 3);
    }

    public function add_class($classes, $cls, $post_id)
    {
        if (self::TYPE !== get_post_type($post_id)) {
            return $classes;
        }

        return array_merge($classes, $this->getSlugs($post_id));
    }

    private function getSlugs($post_id)
    {
        $terms = get_the_terms($post_id, self::TAX);

        if (!$terms || is_wp_error($terms)) {
            return array();
        }

        return wp_list_pluck($terms, 'slug');
    }
}

Then, your loop, you can use post_class to get your classes.

<?php
$projects = new WP_Query(array(
   'post_type' => 'project',
   'nopaging'  => true,
));

while($projects->have_post()): $projects->the_post(); ?>
<li <?php post_class(); ?>>

</li>
<?php endwhile; ?>

Here is the above as a plugin.

0

Here's my answer:

<ul>
                <?php
                    $post = 'project'; // custom post type
                    $i=0;
                    $counter = 0;
                    query_posts("post_type=$post&showposts=-1&order=asc");
                    $post_count = wp_count_posts();
                    while(have_posts()) : the_post();
                    $myExcerpt = get_the_excerpt();
                    $tags = array("<p>", "</p>");
                    $myExcerpt = str_replace($tags, "", $myExcerpt);

                    $slugs = get_terms('project-type');
                    foreach($slugs as $slug) {
                        $slug_array[$i] = $slug->slug;
                        $i++;
                    }
                ?>
                <li class="project <?php echo $slug_array[$counter]; ?>">
                    <a href="<?php the_permalink(); ?>" id="project_<?php echo $id; ?>" class="project_frame">
                        <?php the_post_thumbnail('full', array('class' => 'project_img')); ?>
                        <p style="display: none;"><span class="larger"><?php the_title(); echo $i;?></span><br /><span class="smaller"><?php echo $myExcerpt; ?></span></p>
                    </a>
                </li>
                <?php
                    $counter++;
                    endwhile;
                    wp_reset_query();
                ?>
            </ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.