439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
Pixelous says

This gonna drive me crazy. Can’t make it work :(

<?php if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) : ?>
// script goes here
<?php endif; ?>

But it isn’t works for me. And this:

if ( is_page_template('page-portfolio-2-columns-isotope.php')
        || is_page_template('page-portfolio-3-columns-isotope.php')
        || is_page_template('page-portfolio-4-columns-isotope.php') ) :
            wp_enqueue_script( 'jquery.isotope.min' );
    endif;

works well. How could it be? Any help, guys?

3249 posts Nice Guy
  • Forum Superstar
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
+6 more
RubenBristian says

Why don’t you post the entire code? Maybe you have an issue inside //script goes here .. I have some small snippets of scripts put like that in my themes and they work just fine .. it doesn’t look like any kind of php issue.

Do you see your script outputted in the source code?

439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
Pixelous says

Why don’t you post the entire code? Maybe you have an issue inside //script goes here .. I have some small snippets of scripts put like that in my themes and they work just fine .. it doesn’t look like any kind of php issue. Do you see your script outputted in the source code?

Nope, I don’t see my script outputted in the source code, tha’t the problem. Actually my script is big enough, here it is:

<?php if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) : ?>
        // Isotope
        // modified Isotope methods for gutters in masonry
        jQuery.Isotope.prototype._getMasonryGutterColumns = function() {
            var gutter = this.options.masonry && this.options.masonry.gutterWidth || 0;
                containerWidth = this.element.width();

        this.masonry.columnWidth = this.options.masonry && this.options.masonry.columnWidth ||
                  // or use the size of the first item
                  this.$filteredAtoms.outerWidth(true) ||
                  // if there's no items, use size of container
                  containerWidth;

        this.masonry.columnWidth += gutter;

        this.masonry.cols = Math.floor( ( containerWidth + gutter ) / this.masonry.columnWidth );
        this.masonry.cols = Math.max( this.masonry.cols, 1 );
        };

        jQuery.Isotope.prototype._masonryReset = function() {
            // layout-specific props
            this.masonry = {};
            // FIXME shouldn't have to call this again
            this._getMasonryGutterColumns();
            var i = this.masonry.cols;
            this.masonry.colYs = [];
            while (i--) {
                this.masonry.colYs.push( 0 );
            }
        };

        jQuery.Isotope.prototype._masonryResizeChanged = function() {
            var prevSegments = this.masonry.cols;
            // update cols/rows
            this._getMasonryGutterColumns();
            // return if updated cols/rows is not equal to previous
            return ( this.masonry.cols !== prevSegments );
        };

        // cache container
        var $container = jQuery('#portfolio');
        // initialize isotope
        $container.isotope({
            masonry: {
                columnWidth: <?php if ( is_page_template('page-portfolio-2-columns-isotope.php') ) { ?> 454, <?php } else if ( is_page_template('page-portfolio-3-columns-isotope.php') ) { ?> 292, <?php }  else if ( is_page_template('page-portfolio-4-columns-isotope.php') ) { ?> 212, <?php } ?>
                gutterWidth: 30
            }
        });

        // filter items when filter link is clicked
        jQuery('#filters a').click(function() {
            var selector = jQuery(this).attr('data-filter');
            $container.isotope({ filter: selector });
            return false;
        });

        // set selected menu items
        var $optionSets = jQuery('.option-set'),
            $optionLinks = $optionSets.find('a');

            $optionLinks.click(function(){
                var $this = jQuery(this);
                // don't proceed if already selected
                if ( $this.hasClass('selected') ) {
                    return false;
                }
                var $optionSet = $this.parents('.option-set');
                $optionSet.find('.selected').removeClass('selected');
                $this.addClass('selected'); 
            });
<?php endif; ?>

It’s really strange, it should works fine, but for some reason it doesn’t. A php conditions don’t work.

3249 posts Nice Guy
  • Forum Superstar
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
+6 more
RubenBristian says

Do you have the php conditionals inside the <script> ?

You should make a brand new <script> and put the conditionals before it.. That’s how i do it.

3117 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • Exclusive Author
chrisakelley says
add_action('wp_enqueue_scripts', 'your_prefix_port_scripts');

function your_prefix_port_scripts(){
     if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             wp_enqueue_script( 'jquery.isotope.min' );
     }
}
3249 posts Nice Guy
  • Forum Superstar
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
+6 more
RubenBristian says

add_action('wp_enqueue_scripts', 'your_prefix_port_scripts');

function your_prefix_port_scripts(){
     if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             wp_enqueue_script( 'jquery.isotope.min' );
     }
}
He doesn’t want to enqueue isotope .. he wants to add a small snippet inside the theme. no external js file.
3117 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • Exclusive Author
chrisakelley says


add_action('wp_enqueue_scripts', 'your_prefix_port_scripts');

function your_prefix_port_scripts(){
     if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             wp_enqueue_script( 'jquery.isotope.min' );
     }
}
He doesn’t want to enqueue isotope .. he wants to add a small snippet inside the theme. no external js file.

well thats just a bad idea in the first place, if he needs variables he should be using localized scripts

http://codex.wordpress.org/Function_Reference/wp_localize_script

either way


  if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             put your js here
     }

439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
Pixelous says

Chris, this:

 if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             put your js here
     }

doesn’t work for me, but in this case:

 if ( is_page_template('page-home.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             put your js here
     }

works well only for page-home.php. So, for one template it’s works well but another ignores. How could it be? I really don’t understand, maybe it is a bug?

3117 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • Exclusive Author
chrisakelley says

Chris, this:
 if ( is_page_template('page-portfolio-2-columns-isotope.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             put your js here
     }

doesn’t work for me, but in this case:

 if ( is_page_template('page-home.php')
            || is_page_template('page-portfolio-3-columns-isotope.php')
            || is_page_template('page-portfolio-4-columns-isotope.php') ) {
             put your js here
     }
works well only for page-home.php. So, for one template it’s works well but another ignores. How could it be? I really don’t understand, maybe it is a bug?

do you have any dev tools on? see if its throwing any errors at you

wp_debug in the config file will do enough but theres a ton of plugins to assist with errors

also are you sure those are the correct names?

488 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Won a Competition
  • Referred between 1000 and 1999 users
  • Author had a Free File of the Month
  • Author had a File in an Envato Bundle
  • Bought between 10 and 49 items
+3 more
pixelentity says
echo get_page_template_slug( get_queried_object_id() );
to check if page template slug is correct

BF

by
by
by
by
by
by