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


WordPress: Display particular category on load with filters

我有一个主题,其投资组合使用过滤器(全部,新,旧)来显示类别。但是,在加载时,我希望特定("新")类别是唯一显示的类别。

然后,如果用户希望查看过去的产品,他们会单击"旧"等等。

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

我已经试图找到答案几个小时了,但我还没有能够找到它。

以下是投资组合 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(); ?>

这个问题与WordPress本身无关,而是与您或您的主题用于对数据进行排序的插件有关。

在这种情况下,它使用同位素,这是过滤您在页面上看到的结果的原因。

通过在过滤方面解决同位素的文档,我们看到过滤器可以通过功能或按钮来完成,在这种情况下,我们使用两者的组合,因为我们希望数据在单击时更改,但同时它已经需要过滤,如您所要求的那样。

因此,为了解决这个问题,它是同位素功能文件中的一行:

$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. 如果您更改 slug,这将停止工作,因为它正在检查类"研讨会",如果您这样做,您需要编辑上面提供的代码;
  2. 同位素被屏蔽在主题的自定义jquery文件中,这根本不是一个好的做法,特别是对于维护问题。