Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

can't get categories on portfolio

Open Alex-Ascet opened this issue 11 years ago • 5 comments

Hi there,

We have created extensions>portfolio>shortcodes>portfolio>views>view.php

Whats occuring is the term_id ?> seems to be retuning all categories in every loop leaving the last categorie set for all list items. So all items are being set with the same category. Before this we had an empty category so it was returning a blank string as you suggested.

We need to insure that the loop is only returning the selected category for that item not all of them.

Here is our short code view.

<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}
?>
<?php $fw_ext_projects_gallery_image = fw()->extensions->get( 'portfolio' )->get_config( 'image_sizes' );
$fw_ext_projects_gallery_image = $fw_ext_projects_gallery_image['gallery-image']; ?>

<?php
global $wp_query;
$ext_portfolio_instance = fw()->extensions->get( 'portfolio' );
$ext_portfolio_settings = $ext_portfolio_instance->get_settings();

$taxonomy        = $ext_portfolio_settings['taxonomy_name'];
$term            = get_term_by( 'slug', get_query_var( 'term' ), $taxonomy );
$term_id         = ( ! empty( $term->term_id ) ) ? $term->term_id : 0;
$categories      = fw_ext_portfolio_get_listing_categories( $term_id );

$listing_classes = fw_ext_portfolio_get_sort_classes( $wp_query->posts, $categories );
$loop_data       = array(
    'settings'        => $ext_portfolio_instance->get_settings(),
    'categories'      => $categories,
    'image_sizes'     => $ext_portfolio_instance->get_image_sizes(),
    'listing_classes' => $listing_classes
);
set_query_var( 'fw_portfolio_loop_data', $loop_data );
?>

<?php if ( ! empty( $categories ) ) : ?>
<div class="container">
      <h1 class="section-title-h1 gray">AGG Products</h1>
    </div>
<div class="wrapp-categories-portfolio">
                            <ul id="categories-portfolio" class="portfolio-categories">
                                <li class="filter categories-item" data-filter=".category_all"><a 
                                        href='#' class="link btn"><?php _e( 'All', 'unyson' ); ?></a></li>
                                <?php foreach ( $categories as $category ) : ?>
                                    <li class="filter categories-item"
                                        data-filter=".category_<?php echo $category->term_id ?>"><a
                                            href='#' class="link btn"><?php echo $category->name; ?></a></li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif ?>


<div class="entry-content">
                    <section class="portfolio" id="Container">
                     <div class="w-section w-clearfix products">    
                     <ul id="portfolio-list" class="portfolio-list">        
<?php   
$args = array( 'post_type' => 'fw-portfolio', 'post_per_page' =>'20', 'order' => 'ASC','post_status' => 'publish');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<li class="category_<?php echo $category->term_id ?> w-inline-block item stacked mix category_all portfolio-item">
        <a href="<?php the_permalink() ?>" data-ix="hover-arrow">
            <?php
            $thumbnail_id = get_post_thumbnail_id();
            if( !empty( $thumbnail_id ) ) {
                $thumb_id = get_post_thumbnail_id();
                $image = wp_get_attachment_image_src($thumb_id,'large', true);
            } else {
                $image = fw()->extensions->get('portfolio')->locate_URI('/static/img/no-photo.jpg');
                $thumbnail_title = $image;
            }
            ?>
            <img src="<?php echo $image[0]; ?>" alt="<?php echo $thumbnail_title ?>"/>
            <div class="w-icon-slider-right arrow"></div>
            <h1 class="item-title"><?php the_field('product_title'); ?></h1>
            <div class="data"><?php the_field('product_excerpt'); ?></div>
        </a>
</li>   
    <?php endwhile;?>
</ul>

Thank you for your support.

All the best

Alex-Ascet avatar Jul 20 '15 07:07 Alex-Ascet

Hope it helps: https://github.com/ThemeFuse/Unyson/issues/589

dylan-ngo11 avatar Jul 30 '15 06:07 dylan-ngo11

have you managed to figure this out? for me $categories returns always as an empty array(). I tried to see where this "breaks" and it seems that $categories (from herlper.php) returns

$ext_portfolio_settings = fw()->extensions->get( 'portfolio' )->get_settings();
$taxonomy = $ext_portfolio_settings['taxonomy_name'];
fw_print($ext_portfolio_settings);
fw_print($taxonomy);
$categories = get_terms( $taxonomy, $args );
fw_print($categories);

returns this:

array
(
[post_type] => 'fw-portfolio'
[slug] => 'project'
[taxonomy_slug] => 'portfolio'
[taxonomy_name] => 'fw-portfolio-category'
)

'fw-portfolio-category'

 array()

also, $args is this:

array
(
[hide_empty] => false
[parent] => 3
)

PS: this is not useful : #589

cristeacosmin avatar Oct 06 '15 20:10 cristeacosmin

Try to replace

$categories = get_terms( $taxonomy, $args );

with

$categories = get_terms( $taxonomy );

dylan-ngo11 avatar Oct 07 '15 03:10 dylan-ngo11

that is actually in the framework's helper file found here:

https://github.com/ThemeFuse/Unyson-Portfolio-Extension/blob/master/helpers.php#L33

If I edit this, at the next update the changes would be lost.

cristeacosmin avatar Oct 07 '15 06:10 cristeacosmin

I actually got this fix by creating a function.

  1. Create a helpers.php file inside portfolio extension, not shortcode. Paste this code:
<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

if ( ! function_exists( 'fw_theme_portfolio_post_taxonomies' ) ) :
    function fw_theme_portfolio_post_taxonomies( $post_id, $return = false ) {
        /**
         * Return portfolio post taxonomies
         * @param integer $post_id
         * @param boolean $return
         */
        $taxonomy = 'fw-portfolio-category';
        $terms    = wp_get_post_terms( $post_id, $taxonomy );
        $list     = '';
        $checked  = false;
        foreach ( $terms as $term ) {
            if ( $term->parent == 0 ) {
                // if is checked parent category
                $list .= 'category_' . $term->term_id . ' ';
                $checked = true;
            } else {
                $list .= 'category_' . $term->term_id . ', ';
                $parent_id = $term->parent;
            }
        }

        if ( ! $checked ) {
            // if is not checked parent category extract this parent
            if(isset($parent_id)){
                $term = get_term_by( 'id', $parent_id, $taxonomy );
                $list .= 'category_' . $term->term_id . ' ';
            }
        }

        if ( $return ) {
            return $list;
        } else {
            echo $list;
        }
    }
endif;
  1. Then on your shortcode loop, insert the function.
<li class="mix category_all <?php fw_theme_portfolio_post_taxonomies(get_the_ID()); ?> portfolio-item">

Hope that helps.

jonmlas avatar Nov 17 '15 01:11 jonmlas