将分类插件插入循环类

时间:2012-09-27 作者:rgaspary

我在这里感到很失落,因为我在互联网上看了几十篇教程和帖子,都是关于如何获得分类法的鼻涕虫,而我尝试的似乎都不管用。当我想编辑这篇文章时,分类法显示得很好,而且一切似乎都很好,但是我似乎一辈子都无法让Wordpress给我想要的slug。我真的很沮丧,也很恼火说“对于你已经拥有的一系列帖子,包括每个帖子的分类法”的过程是多么复杂叹气

实际上,我只想得到为每篇文章选择的自定义分类法slug。我有一个公文包页面,我想与jquery交互,我正在尝试使用分类法slug作为jquery脚本的锚。我从Wordpress所需要的就是看到它已经给我提供了所有信息的帖子,并包括它已经链接到的分类法。然后将该段音回音到LI标记的类中。

代码如下:

<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>
希望有人能帮我。

更新时间:

找到了答案。这是正确的代码。

<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>

2 个回复
SO网友:chrisguitarguy

我不会使用query_posts 而是使用post_class 和一个新的WP_Query 对象插入分类法段塞。这更“经得起未来考验”,您可以将其放在插件中,以保持跨主题的相同功能。

这和你现在做的一样(get_the_terms, 等等),但它会将许多积垢排除在模板之外。

<?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\');
    }
}
然后,你的循环,你可以使用post_class 去上课。

<?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; ?>
这是上面的as a plugin.

SO网友:rgaspary

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>
结束

相关推荐