• Hi,
    I have a jQuery script that looks like this:

    $(".toggle").slideUp();
    $(".toggle-link").click(function(){
    	$('.toggle').removeClass("hidden");
        $(this).prev(".toggle").slideToggle("slow");
    	$(this).text($(this).text() == 'Close ✕' ? 'Read more' : 'Close ✕');
      });

    I’ve used a plugin for WordPress called “Ultimate WP Query Search Filter”. To be able to style the output from the filtering I’ve got an add_filter() script in functions.php where I’ve put the html to call the script above:

    add_filter('uwpqsf_result_tempt', 'customize_output', '', 4);
    function customize_output($results , $arg, $id, $getdata ){
    	 // The Query
                $apiclass = new uwpqsfprocess();
                 $query = new WP_Query( $arg );
    		ob_start();	$result = '';
    			// The Loop
    
    		if ( $query->have_posts() ) {
    			while ( $query->have_posts() ) {
    				$query->the_post();global $post;
    					echo  '<a class="toggle-link"><h4>Read more ▾</h4></a>';
    					echo  '<div class="toggle hidden" style="display: none">Hi</div>';			
    
    			}
                            echo  $apiclass->ajax_pagination($arg['paged'],$query->max_num_pages, 4, $id, $getdata);
    		 } else {
    					 echo  'no post found';
    				}
    				/* Restore original Post Data */
    				wp_reset_postdata();
    
    		$results = ob_get_clean();
    			return $results;
    }

    Whenever I use the script and code on other parts of the page it works flawless, but as soon as I put it in functions.php it stop working. Has anyone a clue why it’s doing this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Have you tried debugging this through your browser’s console log? Sounds like you are probably loading it too soon (before jQuery is defined).

    Thread Starter Corneliatt

    (@corneliatt)

    I just checked through the console results:
    the toggle script loads in 28 ms and the 110 ms, but the jquery is before the toggle script in the list? (haven’t looked at this before so can’t really analyze the output 😛 )

    Umm.. could it be some kind of conflict between two jquery links?
    I’ve loaded “my” jquery from: http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js

    and in the list I saw that there was also jquery scripts included from:
    /wp-includes/js/jquery/jquery.js?ver=1.11.1
    /wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1

    But at the same hand it’s working on the other pages. It’s just on this archive page it doesn’t work… Hmm do you have any clue?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    There’s unlikely to be a conflict with the jQuery library, can you link your site so that we can explore this?

    Thread Starter Corneliatt

    (@corneliatt)

    The thing is, I don’t have a web space for it yet, so it’s only local right now.
    Maybe I could add some more code to this thread instead? Or what do you reckon?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    I don’t think we’ll be able to see the issue without context. The most we can spot is errors in the code (and that’s probably not the issue), for example we might say:

    ” Add this bit of code to the top of your jQuery script so that it works in noconflict mode: “

    $ = jQuery;

    But since your jQuery is working elsewhere, I assume that you already have the jQuery working in noconflict mode. So that’s not the issue.

    Thread Starter Corneliatt

    (@corneliatt)

    Ok, so now I know that at least the first part of the toggle script works. I forgot to show you that part earlier:

    $(document).ready(function(){
        $(this).scrollTop(0);
    	$('.toggle').addClass("hidden");
    });

    The toggle div has the hidden class, so that works. So it must be link function that somehow doesn’t want to “execute” on click. I tried to put the link and the toggle div in the same echo, thought it could be something with the .prev(), but no… Have also tried to change to .next() but nothing…

    Gahh, this drives me crazy!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘jQuery slideUp() functions.php for archive page’ is closed to new replies.