Show only 1 category on page

Asked

Viewed 99 times

1

Need to create 2 functions, each will show a category (commercial and residential)

    <?php

class MP_Emmet_Plugin_Portfolio {

    public function __construct() {
        add_action( 'mp_emmet_section_portfolio', array( $this, 'get_html' ) );
    }

    /*
     * Get default portfolio
     */

    public function get_default_portfolio() {
        $mp_emmet_portfolio_animation = esc_attr( get_theme_mod( 'theme_portfolio_animation', 'fadeInLeft' ) );
        ?>
        <div class="portfolio-list">
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio1.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Logotypes', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio2.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Web-design', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio3.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Print', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio4.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Aplications', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio5.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Aplications', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio6.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'llustrations', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio7.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Web-design', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="portfolio-box  <?php
            if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
            endif;
            ?>">
                <a href="#" class="portfolio-content">
                    <img src="<?php echo MP_EMMET_PLUGIN_PATH . 'images/portfolio8.jpg'; ?>"
                         class="attachment-thumb-portfolio wp-post-image"
                         alt="<?php _e( 'Fitsy logo', 'mp-emmet' ); ?>">
                    <div class="portfolio-hover">
                        <div class="hover-content">
                            <div>
                                <h5 class="portfolio-title"><?php _e( 'Fitsy logo', 'mp-emmet' ); ?></h5>
                                <div class="portfolio-categories">
                                    <span><?php _e( 'Your category', 'mp-emmet' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
            <div class="clearfix"></div>
        </div>
        <?php
    }

    /*
     * Get portfolio
     */

    public function get_portfolio() {
        $mp_emmet_portfolio_animation = esc_attr( get_theme_mod( 'theme_portfolio_animation', 'fadeInLeft' ) );
        $posts_per_page               = apply_filters( 'mp_emmet_portfolio_posts_per_page', 8 );
        $args                         = array(
            'post_type'      => 'portfolio',
            'posts_per_page' => $posts_per_page
        );
        $prizes                       = new WP_Query( $args );
        if ( $prizes->have_posts() ) {
            ?>
            <div class="portfolio-list ">
                <?php
                while ( $prizes->have_posts() ) {
                    $prizes->the_post();
                    ?>
                    <div class="portfolio-box  <?php
                    if ( $mp_emmet_portfolio_animation != 'none' ): echo 'animated anHidden"  data-animation="' . $mp_emmet_portfolio_animation;
                    endif;
                    ?>">
                        <a href="<?php the_permalink(); ?>" class="portfolio-content">
                            <?php
                            if ( has_post_thumbnail() ) {
                                the_post_thumbnail( 'mp-emmet-thumb-medium' );
                            } else {
                                ?>
                                <div class="portfolio-empty-thumbnail">
                                    <span class="date-post">
                                        <?php echo get_post_time( 'j M' ); ?>
                                    </span>
                                </div>
                                <?php
                            }
                            ?>
                            <div class="portfolio-hover">
                                <div class="hover-content">
                                    <div>
                                        <h5 class="portfolio-title"><?php the_title(); ?></h5>
                                        <?php
                                        $post_categories = wp_get_post_terms( get_the_ID(), 'portfolio_tag' );
                                        if ( ! empty( $post_categories ) ) :
                                            ?>
                                            <div class="portfolio-categories">
                                                <?php
                                                $last_key = count( $post_categories );
                                                $i        = 0;
                                                foreach ( $post_categories as $cat ) {
                                                    echo '<span>' . $cat->name . '</span>';
                                                    if ( ++ $i != $last_key ) {
                                                        echo '<span>,</span> ';
                                                    }
                                                }
                                                ?>
                                            </div>
                                            <?php
                                        endif;
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php }
                ?>
                <div class="clearfix"></div>
            </div>
            <?php
        } else {
            $this->get_default_portfolio();
        }
    }

    /*
     * Get title
     */

    public function get_title() {
        $mp_emmet_portfolio_title = esc_html( get_theme_mod( 'theme_portfolio_title' ) );
        if ( get_theme_mod( 'theme_portfolio_title', false ) === false ) :
            ?>
            <h2 class="section-title"><?php _e( 'our portfolio', 'mp-emmet' ); ?></h2>
            <?php
        else:
            if ( ! empty( $mp_emmet_portfolio_title ) ):
                ?>
                <h2 class="section-title"><?php echo $mp_emmet_portfolio_title; ?></h2>
                <?php
            endif;
        endif;
    }

    /*
     * Get description
     */

    public function get_description() {
        $mp_emmet_portfolio_description = wp_kses( get_theme_mod( 'theme_portfolio_description' ), mp_emmet_plugin_allowed_html() );

        if ( get_theme_mod( 'theme_portfolio_description', false ) === false ) :
            ?>
            <div
                class="section-description"><?php _e( 'In the portfolio section you can display your works consisting of screenshots and additional information', 'mp-emmet' ); ?></div>
            <?php
        else:
            if ( ! empty( $mp_emmet_portfolio_description ) ):
                ?>
                <div class="section-description"><?php echo $mp_emmet_portfolio_description; ?></div>
                <?php
            endif;
        endif;
    }

    /*
     * Get Buttons
     */

    public function get_buttons() {
        $mp_emmet_portfolio_button_label = esc_html( get_theme_mod( 'theme_portfolio_button_label', __( 'check all works', 'mp-emmet' ) ) );
        $mp_emmet_portfolio_button_url   = esc_url( get_theme_mod( 'theme_portfolio_button_url', '#portfolio' ) );
        ?>
        <div class="section-buttons">
            <?php
            if ( ! empty( $mp_emmet_portfolio_button_label ) && ! empty( $mp_emmet_portfolio_button_url ) ):
                ?>
                <a href="<?php echo $mp_emmet_portfolio_button_url; ?>"
                   title="<?php echo $mp_emmet_portfolio_button_label; ?>" class="button white-button">
                    <?php echo $mp_emmet_portfolio_button_label; ?></a>
                <?php
            endif;
            ?>
        </div>
        <?php
    }

    /*
     * features section html
     */

    public function get_html() {
        $mp_emmet_portfolio_id_option = esc_attr( get_theme_mod( 'theme_portfolio_id' ) );
        $mp_emmet_portfolio_id        = empty( $mp_emmet_portfolio_id_option ) ? 'portfolio' : esc_attr( get_theme_mod( 'theme_portfolio_id' ) );
        ?>
        <section id="<?php echo $mp_emmet_portfolio_id; ?>" class="portfolio-section white-section default-section">
            <div class="container">
                <div class="section-content">
                    <?php
                    $this->get_title();
                    $this->get_description();
                    $this->get_portfolio();
                    $this->get_buttons();
                    ?>
                </div>
            </div>
        </section>
        <?php
    }
}
  • you’re using pages to show categories?

  • That, I created 2 wordpress page templates, each to show a category.

  • In this code is showing right query_posts(array('portfolio_category' => 'commercial', 'posts_per_page' => 0)));

  • In this filter you want only the posts that have ONLY the 'commercial' category, that’s it?

  • @Hamurabiaraujo this

1 answer

2

No need to use pages or query_posts() for this*. Use the Hierarchy of Templates wordpress.

inserir a descrição da imagem aqui

You must have a generic list file like archive.php, or even the index.php. Create a copy of it and rename as taxonomy-portfolio_category.php.

Ready, it will be loaded when you are listing any term in taxonomy portfolio_category. If you need a template for the term only comercial, use taxonomy-portfolio_category-comercial.php

  • In this case, I could be using the link that wordpress generates to store posts from each category ( www.meusite.com.br/portofolio-Category/commercial ). The problem is that I’m using a section of the theme that brings these posts ( if (has_action('mp_emmet_section_portfolio') { do_action('mp_emmet_section_portfolio'); } ) . That is, I am not using the abse of Archive.php to show but the section of the site. I created a template with this section, now only need to separate by categories.

  • I copied the template of the page I made, using the section of the site and put the name taxonomy-portfolio_category-commercial.php. He’s taking the post titles and showing them on the page title. I’ll put the whole code in the post edition here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.