个性化阅读
专注于IT技术分析

WordPress:使用过滤器在加载时显示特定类别

我有一个包含使用过滤器(ALL, NEW, OLD)显示类别的投资组合的主题。但是, 在加载时, 我希望将特定(“新”)类别显示为”唯一”类别。

然后, 如果用户希望查看过去的产品, 则可以单击”旧”, 依此类推。

请记住, 我将创建另一个投资组合, 并且投资组合的新项目将仅在加载时显示。

我已经尝试了好几个小时才能找到答案, 但还没有找到答案。

这是投资组合php页面的代码:

    <?php
    /**
     * Taxonomy Template for the Portfolios
    */
    ?>
    <?php get_header(); ?>

    <?php

    if ( $post ) :

    $port = wp_get_post_terms( $post->ID, array('portfolios') );
    $portfolio = $port[0];

    $post_img_width = "";
    $post_img_height = "";

    global $mav_data; // fetch options stored in $mav_data

    $portfolio_order_1 = $mav_data['portfolio_order_1']; // date, title
    $portfolio_order_2 = $mav_data['portfolio_order_2']; // ASC, DESC

    $tax_query[] = array(
        //'taxonomy' => $wp_query->query_vars['taxonomy'], 'taxonomy' => 'portfolios', 'field' => 'slug', 'terms' => array( $portfolio->slug )
    );

    $args = array(
        'post_type'=> 'project', 'posts_per_page'=> -1, 'orderby' => $portfolio_order_1, 'order' => $portfolio_order_2, 'tax_query' => $tax_query
    );

    $port_query = new WP_Query($args);

    ?>

    <?php
    $tag_extra_fields = get_option('portfolios_fields');
    $portfolio_layout = $tag_extra_fields[$portfolio->term_id]['_portfolio_layout'];
    ?>
    <section id="content" class="portfolio one-column <?php echo esc_attr( $portfolio_layout ); ?>" role="main">

        <div class="wrapper clearfix">

        <header class="page-header">

            <h1 class="entry-title"><?php echo( $portfolio->name ); ?></h1>

            <?php if ($portfolio->description) { ?><p class="portfolio-header-description"><?php echo( $portfolio->description ); ?></p><?php } ?>

            <?php

            $temp = array();

            foreach ( $posts as $post ) {

                $temp_args = array();

                while ($port_query->have_posts()) : $port_query->the_post(); // the loop begins, we need it here. It's important!!

                $temp_cats = wp_get_object_terms( $post->ID, 'project_category'/*, $args*/ );

                if ( $temp_cats ) {
                    foreach ( $temp_cats as $temp_cat ) {
                        if ( ! in_array( $temp_cat->slug, $temp ) ) {
                            $temp[] = $temp_cat->slug;
                            $categories[] = $temp_cat;
                        }
                    }
                }

                endwhile;

            }

            // DAHEX
            $temp = array();

            if(!empty($categories)) {
                foreach ( $categories as $category ) {
                    $temp[] = array ('term_id'=>$category->term_id, 'name'=>$category->name, 'slug'=>$category->slug, 'term_group'=>$category->term_group, 'term_taxonomy_id'=>$category->term_taxonomy_id, 'taxonomy'=>$category->taxonomy, 'description'=> $category->description, 'parent'=>$category->parent, 'count'=> $category->count);
                }
            }

            usort($temp, array(new Sorter('slug'), 'sort')); // Sorting Array by slug

            $categories = array();
            foreach ( $temp as $category ){
                $categories[] =(object) $category;
            }
            // DAHEX

            if(!empty($categories)) {

                if(!is_wp_error( $categories )){

                    echo( '<ul id="filters" class="option-set">
                    <li><a href="#" data-filter="*" class="show-all selected">All</a></li>' );

                    foreach ( $categories as $category ) {
                        echo '<li><span class="sep">/</span><a href="#' . $category->slug . '" data-filter=".' . $category->slug . '">' . $category->name . '</a></li>';
                    }

                    echo( '</ul> <!-- /end #filters -->' );

                } else {
                    echo '<span class="ooops">';
                    _e( 'Ooops! nothing found...', 'mav_framework' );
                    echo '</span>';
                }
            }
            ?>
        </header> <!-- /end .page-header -->

        <section id="projects">

            <?php
            while ($port_query->have_posts()) : $port_query->the_post(); // the loop begins
            $terms = get_the_terms( get_the_ID(), 'project_category' );
            $terms = $terms == false ? array() : $terms;
            ?>

            <?php
            $custom = get_post_custom($post->ID);

            $portfolio_permalink = $custom["project_permalink"][0];
            $portfolio_desc = $custom["project_desc"][0];

            if ( !isset( $lightbox_path ) ) {
                $lightbox_path = '';
            }

            // Prepare Project Image Thumb
            $project_img = $custom["project_img"][0];
            $project_img_ID = $custom['project_img_ID'][0];

            // Need some proof check to ensure that no "notice" is thrown ...
            if ( ! empty( $portfolio ) ) {

                $term_slug = $portfolio->slug;

                if ( isset( $custom["lightbox_path"][0] ) ) {
                    $lightbox_path = $custom["lightbox_path"][0];
                } else {
                    $lightbox_path = '';
                }

                $empty_thumb = '<img class="portfolios_single_thumb portfolio-image" src="' . get_template_directory_uri() . '/images/thumb.png" width="' . $post_img_width . '" height="' . $post_img_height . '" alt="' . $post->post_title . '" />';

                if ( isset( $project_img_ID ) ) {

                    if ( is_numeric( $project_img_ID ) ) {

                        $thumb_ID = $project_img_ID;
                        $thumb = wp_get_attachment_image( $thumb_ID, 'mav-thumbnails', false, array( 'class' => 'portfolios_post_image_thumb portfolio-image', 'alt' =>  $post->post_title ) );

                        if ( empty ($thumb) ) {
                            $thumb = $empty_thumb;
                        }

                    } elseif( $project_img_ID != "" ) {

                        $thumb = '<div class="project_iframe_thumb-$term_slug"><iframe width="' . $post_img_width . '" height="' . $post_img_height . '" src="' . $project_img . '" title="' . $project_img_ID . '" frameborder="0" allowfullscreen></iframe></div>';

                    } else {

                        $thumb = $empty_thumb;

                    }

                } else {
                    $thumb = $empty_thumb;
                }

            }
            ?>

            <article id="project-<?php the_ID(); ?>" class="element <?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->slug)). ' '; } ?>">

                <?php
                /**
                 * Generate the Project Image (Thumb)
                 */
                if ( $lightbox_path != '' ) { ?>
                <figure class="thumb-container">
                    <a href="<?php echo esc_attr( $lightbox_path ); ?>" data-rel="prettyPhoto" title="<?php the_title_attribute(); ?>">
                        <span class="overlay lightbox"></span>
                        <?php mav_framework_project_label( $post, array( 'portfolios-project-label' ) ); ?>
                        <?php echo( $thumb ); ?>
                    </a>
                </figure> <!-- /end .thumb-container -->
                <?php
                } elseif ($portfolio_permalink) {
                ?>
                <figure class="thumb-container">
                    <a target="_blank" href="<?php echo esc_attr( $portfolio_permalink ); ?>" rel="bookmark">
                        <span class="overlay link"></span>
                        <?php mav_framework_project_label( $post, array( 'portfolios-project-label' ) ); ?>
                        <?php echo( $thumb ); ?>
                    </a>
                </figure> <!-- /end .thumb-container -->
                <?php } else { ?>
                <figure class="thumb-container">
                    <a href="<?php the_permalink() ?>" rel="bookmark">
                        <span class="overlay">
                            <span class="view"><?php _e( 'View', 'mav_framework' ); ?></span>
                        </span>
                        <?php mav_framework_project_label( $post, array( 'portfolios-project-label' ) ); ?>
                        <?php echo( $thumb ); ?>
                    </a>
                </figure> <!-- /end .thumb-container -->
                <?php } // end Generate the Project Image (Thumb) ?>

                <h2 class="project-title">
                    <?php if ($portfolio_permalink) { ?>
                    <a target="_blank" href="<?php echo esc_url( $portfolio_permalink ); ?>" rel="bookmark"><?php the_title(); ?></a>
                    <?php } else { ?>
                    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
                    <?php } ?>
                </h2>

                <?php if ($portfolio_desc) { ?><p class="project-description"><?php echo do_shortcode(stripslashes($portfolio_desc)); ?></p><?php } ?>


                <footer class="entry-meta">

                    <span class="posted-on">
                        <?php _e( '<span class="posted-on-title">Posted on: </span>', 'mav_framework' ); ?><a href="<?php the_permalink() ?>"><?php /* http://codex.wordpress.org/Formatting_Date_and_Time */ echo get_the_date('F j, Y'); ?></a>
                    </span> <!-- /end .posted-on -->

                    <?php
                    $project_categories = wp_get_object_terms($post->ID, 'project_category');
                    if ($project_categories) { ?>
                    <span class="cat-links">
                        <?php
                            _e( '<span class="cat-links-title">Category: </span>', 'mav_framework' );
                            $project_category = array();
                            foreach($project_categories as $category) {
                                $project_category[] = '<a href="'.get_home_url().'/?project_category=' . $category->slug . '">' . $category->name . '</a>';
                            }
                            echo implode(', ', $project_category);
                        ?>
                    </span> <!-- /end .cat-links -->
                    <?php } ?>

                    <?php // Project Tags
                    $project_tags = wp_get_object_terms($post->ID, 'project_tag');
                    if ($project_tags) {
                        $project_tag = array();
                        foreach($project_tags as $tag) {
                            $project_tag[] = '<a href="'.get_home_url().'/?project_tag=' . $tag->slug . '">' . $tag->name . '</a>';
                        } ?>
                        <span class="tag-links">
                            <?php _e( '<span class="tag-links-title">Tagged: </span>', 'mav_framework' ); ?> <?php echo implode(', ', $project_tag); ?>
                        </span> <!-- /end .tag-links -->
                    <?php } ?>

                </footer> <!-- /end .entry-meta -->

            </article> <!-- /end #project-<?php the_ID(); ?> .element -->

            <?php endwhile; ?>

            <?php wp_reset_query(); ?>

        </section> <!-- /end #projects -->

        </div> <!-- /end .wrapper -->

    </section> <!-- /end #content -->

<?php endif; ?>

<?php
/**
 * Add portfolio bottom quote
 */
if ( $mav_data['portfolio_quote_text'] ) : ?>
<section id="portfolio-quote" class="clearfix">
    <div class="wrapper clearfix">
    <?php get_template_part( 'inc/block_portfolio_quote' ); ?>
    </div> <!-- /end .wrapper -->
</section> <!-- /end #portfolio-quote -->
<?php endif; ?>

<?php get_footer(); ?>

#1


此问题与Wordpress本身无关, 但与你或主题用于对数据进行排序的插件无关。

在这种情况下, 它使用的是同位素, 这就是过滤你在页面上看到的结果的方法。

通过在过滤方面访问Isotope的文档, 我们看到可以通过功能或按钮来完成过滤, 在这种情况下, 我们将两者结合使用, 因为我们希望在单击时更改数据, 但同时它已经需要按照你的要求进行过滤。

因此, 要解决该问题, 它是同位素功能文件中的一行:

$container.isotope({
            itemSelector : '.element', filter: '.workshops'
        });

我刚刚添加了最后一个。

除此之外, 你只需要破解你提供的代码, 即可将” selected”类添加到按钮:

echo( '<ul id="filters" class="option-set">

                        <li><a href="#" data-filter="*" class="show-all">All</a></li>' );

                        foreach ( $categories as $category ) { ?>

                        <li>
                            <span class="sep">/</span>
                            <a href="#<?php echo $category->slug; ?>" <?php if($category->slug == 'workshops'): echo 'class="selected"'; endif;?>data-filter=".<?php echo $category->slug; ?>"><?php echo $category->name;?></a>
                        </li>

                        <?php 
                        }

                        echo( '</ul>' );

现在有一些注意事项:

  1. 如果你更改了子弹, 它将停止工作, 因为它正在检查”车间”类, 如果你这样做, 则需要编辑上面提供的代码;
  2. 同位素在主题的自定义jquery文件中被屏蔽, 这根本不是一个好习惯, 特别是对于维护问题。
赞(0)
未经允许不得转载:srcmini » WordPress:使用过滤器在加载时显示特定类别

评论 抢沙发

评论前必须登录!