将所有类别作为类添加到Foreach循环中

时间:2019-06-26 作者:jlock

我正在尝试修改一个具有过滤功能的公文包插件。用于列出要发布的项目的当前代码为:

<?php

class FullwidthPortfolioGallery extends ET_Builder_Module {
    public $slug       = \'fullwidth_portfolio_gallery\';
    public $vb_support = \'on\';

    protected $module_credits = array(
        \'module_uri\' => \'\',
        \'author\' => \'\',
        \'author_uri\' => \'\',
    );

    public function init() {
        $this->name = esc_html__(\'Portfolio Gallery\', \'divi-modules\');
        $this->fullwidth = true;

        $this->advanced_fields = [
            \'background\' => false,
            \'fonts\' => false,
            \'max_width\' => false,
            \'link_options\' => false,
        ];
    }

    public function get_fields() {
        return [];
    }

    public function render($attrs, $content = null, $render_slug) {

        global $post;
        $portfolio = [];
        $post_args = [
            \'posts_per_page\' => -1,
            \'orderby\'        => \'date\',
            \'order\'          => \'DESC\',
            \'post_type\'      => \'portfolio\',
        ];

        foreach (get_posts($post_args) as $post) {

            $new = new \\stdClass;

            $images = get_post_meta($post->ID, \'portfolio_photos\', true);
            $new->images = [];

            if (empty($images)) {
                continue;
            } else {
                foreach($images as $k => $img) {
                    $i = wp_get_attachment_image_src($k, \'portfolio_gallery_size\');
                    $new->images[] = (object) [
                        \'src\' => $i[0],
                        \'w\' => $i[1],
                        \'h\' => $i[2],
                    ];
                }
            }

            $billboard_image = array_rand($images, 1);
            $new->billboard = new \\stdClass;
            $new->billboard->mobile = wp_get_attachment_image_src($billboard_image, \'portfolio_billboard_mobile\')[0];
            $new->billboard->desktop = wp_get_attachment_image_src($billboard_image, \'portfolio_billboard_desktop\')[0];

            $new->title = $post->post_title;
            $new->category = wp_get_post_terms($post->ID, \'portfolio_category\')[0]->name;
            $new->category_slug = wp_get_post_terms($post->ID, \'portfolio_category\')[0]->slug;
            $new->lightbox = \'lightbox-\' . $post->ID;
            $new->id = $post->ID;

            $portfolio[] = $new;
            unset($new);
            unset($images);

        }

        if (empty($portfolio)) {
            return;
        }

        add_action(\'wp_footer\', function() {
            include plugin_dir_path( dirname( __FILE__ ) ) . \'../template/photoswipe.html\';
        });

        wp_register_script(\'isotope\', \'https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js\', [\'jquery\'], false, true);
        wp_register_script(\'portfolio_scripts\', plugins_url( \'FullwidthPortfolioGallery.js\', __FILE__), [\'isotope\', \'jquery\'], false, true);

        $portfolio_items = [];
        foreach ($portfolio as $p) {
            $portfolio_items[$p->id] = $p;
        }
        wp_localize_script(\'portfolio_scripts\', \'portfolio_items\', $portfolio_items);
        wp_enqueue_script(\'isotope\');
        wp_enqueue_script(\'portfolio_scripts\');

        $categories = get_terms( [
            \'taxonomy\' => \'portfolio_category\',
            \'hide_empty\' => true,
        ] );

        $html = \'<div class="portfolio-categories"><div class="portfolio-categories-wrap"><div class="portfolio-categories-list container">\';

        $html .= \'<button class="toggle"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-filter"></span></button>\';
        $html .= \'<button class="filter active" data-filter="*">Vis alle</button>\';

        foreach ($categories as $cat) {
            $html .= \'<button data-filter=".filter-\'.$cat->slug.\'" class="filter">\'.$cat->name.\'</button>\';
        }

        $html .= \'</div></div></div>\';

        $html .= \'<div class="portfolio-list"><div class="portfolio-list-wrap">\';
        foreach ($portfolio as $p) {
            $html .= \'<div class="portfolio filter-\'.$p->category_slug.\'" data-id="\'.$p->id.\'"><div class="portfolio-wrap">\';
            $html .= \'<div class="spinner-wrapper"><div class="spinner-wrap"><div class="spinner"></div></div></div>\';
            $html .= \'<div class="billboard"><img class="lazy mobile" data-src="\'.$p->billboard->mobile.\'" alt="\'.$p->title.\'" /><img class="lazy desktop" data-src="\'.$p->billboard->desktop.\'" alt="\'.$p->title.\'" /><div class="overlay"><span class="dashicons dashicons-images-alt"></span></div></div>\';
            $html .= \'<div class="info"><p class="cat">\'.$p->category.\'</p><h2>\'.$p->title.\'</h2></div>\';
            $html .= \'</div></div>\';
        }
        $html .= \'</div></div>\';

        return \'<div class="fullwidth-portfolio-gallery">\'.$html.\'</div>\';
    }
}

new FullwidthPortfolioGallery;
目前,每个项目只有一个类“filter Termslaug”,但我想为项目所在的所有类别列出类。

有人能帮我吗?

顺致敬意,

2 个回复
最合适的回答,由SO网友:Bhupen 整理而成

请更换:

$new->category_slug = wp_get_post_terms($post->ID, \'portfolio_category\')[0]->slug;
使用

$category_slug = wp_get_post_terms($post->ID, \'portfolio_category\');
$cat_cls = \'\';
foreach($category_slug as $cat_classes){
    $cat_cls .= " " . $cat_classes->slug;
}
$new->category_slug = $cat_cls;
因为上面编写的代码将只存储它将获得的第一个类别。

SO网友:ChristopherJones

当你在你的$categories, 您可以构建一个类以供以后使用。我只提取了两个foreach循环,并在其中添加了几行,希望您能够完成:

// Fun stuff above

$category_class = \'\';
foreach ($categories as $cat) {
    $html .= \'<button data-filter=".filter-\'.$cat->slug.\'" class="filter">\'.$cat->name.\'</button>\';
    // NOTE: Build out your $category_class here
    $category_class .= $cat->slug.\' \'; // we need an extra space to separate each class. We\'ll use trim() to clean up the extra space to the right of the last class
}

$html .= \'</div></div></div>\';

$html .= \'<div class="portfolio-list"><div class="portfolio-list-wrap">\';
foreach ($portfolio as $p) {
    $html .= \'<div class="portfolio filter-\'.$p->category_slug.\' \'.trim($category_class).\'" data-id="\'.$p->id.\'"><div class="portfolio-wrap">\';
    $html .= \'<div class="spinner-wrapper"><div class="spinner-wrap"><div class="spinner"></div></div></div>\';
    $html .= \'<div class="billboard"><img class="lazy mobile" data-src="\'.$p->billboard->mobile.\'" alt="\'.$p->title.\'" /><img class="lazy desktop" data-src="\'.$p->billboard->desktop.\'" alt="\'.$p->title.\'" /><div class="overlay"><span class="dashicons dashicons-images-alt"></span></div></div>\';
    $html .= \'<div class="info"><p class="cat">\'.$p->category.\'</p><h2>\'.$p->title.\'</h2></div>\';
    $html .= \'</div></div>\';
}

// Fun stuff below
希望有帮助!

相关推荐

Get category url in loop

我需要获取类别url,以便将其放入元代码段中。现在,span内的输出是带有url的标记。我只需要获取不带标记的url,并将其放入$cat\\u display中。我尝试使用2个选项,但会看到一个错误:注意:尝试获取非对象的属性// Get post category info $category = get_the_category(); if(!empty($category)) { // Get last category post is in