0
$('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');

the above is a code from the .js file of WP infinite scroll Wordpress plugin.

Can we somehow put a PHP condition inside a js file:

If( logical condition 1 )  {
    $('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');
} elseif( logical condition 2 )  {
    $('.class2').append( '<span class="load-more"></span>' );
    var button = $('.class2 .load-more');
}

P.S.: PHP logic should go inside a js file not script<></script> tags.

  • [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – t.niese Feb 11 '18 at 16:32
  • I think it's not duplicate here she is not dealing with code written in HTML file in ` – WordCent Feb 11 '18 at 16:33
  • 1
    @somethingnow the problem is still the same. PHP is server side code. PHP can be used to compose any kind of file (html, css, js) server side, and then send it to the client. But it is a server side php file that holds the php condition and then builds a JS file out of it. – t.niese Feb 11 '18 at 16:37
  • @t.niese suggest a solution to her. – WordCent Feb 11 '18 at 16:40
  • [Executing PHP code inside a .js file](https://stackoverflow.com/questions/23574306) – t.niese Feb 11 '18 at 16:41
  • This entirely depends on what condition you want to check and whether that's even possible given the [difference between server and client side programming](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). At least you're probably approaching the problem the wrong way. It's hard to suggest the *right* way without more details. – deceze Feb 11 '18 at 16:41
  • It is based on what we have seklected in theme customizer. Condition will be like this ☻6 http://aristath.github.io/kirki/docs/controls/select.html. Class will change based on the condition selcted in theme customizer of WP. – wordpressgirl Feb 11 '18 at 16:44

1 Answers1

0

define a other js file example condition.js

----- condition.js

<?php 
echo 'condition ..... ;
?>

---- condition.js

// condution.js output 
var lc = [ function(){return false;} , function(){ return x *y; }  ];

.

if( lc[0]() )  {
    $('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');
} elseif( lc[1]() )  {
    $('.class2').append( '<span class="load-more"></span>' );
    var button = $('.class2 .load-more');
}

---- OR -----

// condution.js output
var lc = [ true , 0 ]; 

After

if( lc[0] )  {
    $('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');
} elseif( lc[1] )  {
    $('.class2').append( '<span class="load-more"></span>' );
    var button = $('.class2 .load-more');
}
Hasan Delibaş
  • 470
  • 3
  • 14