2

I made custom page template Which displays all the subpages of the 1st level pages in tabs with nested accordions with second level subpages. Here is my page : http://avgustin.dn.ua/en/menu/

Because of this problem - jquery ui accordions within tabs I can't use plugin JQuery UI because it initialize tabs before accordions by default. So I add jquery manualy like so (I edit that like in folowing advice in function.php):

    function my_enqueue_jquery_ui() {
    wp_enqueue_script( 'jquery-ui-1.8.2', '/wp-content/themes/papyrus/js/jquery-1.8.2.js', '1.8.2', array( 'jquery' ) );
}
function my_enqueue_jquery_ui2() {
    wp_enqueue_script( 'jquery-ui-1.9.1.custom', '/wp-content/themes/papyrus/js/jquery-ui-1.9.1.custom.js', '1.9.1', array( 'jquery' ) );
}
add_action('wp_enqueue_scripts', 'my_enqueue_jquery_ui');
add_action('wp_enqueue_scripts', 'my_enqueue_jquery_ui2');

and than initialize accordions before tabs

jQuery(document).ready(function($) {    
    <?php
        foreach( $mypages as $page ) {      
            $content = $page->post_content;
            if ( ! $content ) // Check for empty page
                continue;?>
            $("#accordion<?php echo $page->ID ?>").accordion({collapsible: true, active: -10 });    
        <?php 
                }
        ?>      
              $("#tabs").tabs();     
    });

Everything works fine until I activate any plugin that uses jquery, for example flash gallery. How can I add custom jquery support to page template in the rigth way. Is it real to make universal working page template with jquery, that can be used whith any template without modifications? Help me please! Here is the complete code

<?php
/*
Template Name: Menu_page
*/
?><?php get_header(); ?>
  <div id="content">
  <div class="entry"/>
  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
        <h2><?php the_title(); ?></h2>
        <div class="post-content">        
<?php
    $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc', 'parent' =>  $post->ID) );?>
    <div id="tabs"> 
       <ul>
<?php
    foreach( $mypages as $page ) {      
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;
        $content = apply_filters( 'the_content', $content );
    ?>
         <li><a href="#tab<?php echo $page->ID ?>"><?php echo $page->post_title; ?></a></li>        
    <?php   
    }?>
</ul>
<?php
    foreach( $mypages as $page ) {      
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;
        $content = apply_filters( 'the_content', $content );
    ?>
         <div id="tab<?php echo $page->ID ?>">
         <?php echo $content;?>
         <div id="accordion<?php echo $page->ID ?>">
         <?php 
         $mypages2 = get_pages( array( 'child_of' => $page->ID, 'sort_column' => 'menu_order', 'sort_order' => 'asc', 'parent' =>  $page->ID) );
         foreach( $mypages2 as $page2 ) {
            $content2 = $page2->post_content;
            if ( ! $content2 ) // Check for empty page
                continue;
            $content2 = apply_filters( 'the_content', $content2 );?>

            <h3><a href="#"><?php echo $page2->post_title; ?></a></h3>
            <div>   
                <?php echo $content2;?>
            </div>
            <?php }?>
            </div>
            </div>
    <?php   
    }?></div>     


<script>        
jQuery(document).ready(function($) {    
<?php
    foreach( $mypages as $page ) {      
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;?>
        $("#accordion<?php echo $page->ID ?>").accordion({collapsible: true, active: -10 });    
    <?php 
            }
    ?>      
          $("#tabs").tabs();     
});
</script>       
            <?php the_content('<p class="serif">Читать полностью &raquo;</p>'); ?>
            <?php link_pages('<p><strong>Страницы:</strong> ', '</p>', 'number'); ?>
        </div>
    </div>

  <?php endwhile; endif; ?>
    </div>
  </div><!--/content -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

When I activate other plugin (flash galery) in source code in chrome I see:

<script type='text/javascript' src='http://avgustin.dn.ua/wp-includes/js/jquery/jquery.js?ver=1.7.2'></script>

after my lines with jquery initialize. And so my jquery does not work :(

Community
  • 1
  • 1
BLiN
  • 61
  • 6

1 Answers1

0

Why don't you try including jQuery via the wp_enqueue_script() function? Like so:

function my_enqueue_jquery_ui() {
    wp_enqueue_script( 'jquery-ui-1.9.1.custom', '/wp-content/uploads/jquery-ui-1.9.1.custom/js/jquery-ui-1.9.1.custom.js', '1.9.1', array( 'jquery' ) );
}
add_action('wp_enqueue_scripts', 'my_enqueue_jquery_ui');

From what I understood your problem is that other plugins require jQuery and then both of them conflict(one over-writes the other) and your code doesn't work properly any more.

Also I would consider keeping the jQuery UI script in the theme directory, but that's up to you.

Nikola Ivanov Nikolov
  • 5,022
  • 1
  • 26
  • 27
  • I fixed as you said:function my_enqueue_jquery_ui() { wp_enqueue_script( 'jquery-ui-1.8.2', '/js/jquery-ui-1.8.2.js', '1.8.2', array( 'jquery' ) ); } add_action('wp_enqueue_scripts', 'my_enqueue_jquery_ui'); and get this erors in console: Uncaught ReferenceError: add_action is not defined avgustin.dn.ua:170 Uncaught ReferenceError: jQuery is not defined – BLiN Nov 16 '12 at 16:17
  • Is it still not working? If so, could you give me a link to your page? – Nikola Ivanov Nikolov Nov 16 '12 at 16:19
  • Oh, sorry if I confused you. This is PHP code and it should reside in the theme's `functions.php` file. – Nikola Ivanov Nikolov Nov 16 '12 at 16:21
  • Can I not use functions.php? or this is the only rigth way? – BLiN Nov 16 '12 at 16:28
  • I know there is a way to include jQuery only if it's not present(you would check for the `jQuery` object and if it's not defined, you create a `script` tag), but by using the function that I posted, you will leave WordPress to make sure that jQuery is not included twice on the same page. – Nikola Ivanov Nikolov Nov 16 '12 at 16:31
  • I edited functions.php like you adviced, it works again! But it didn't help :( I turned on flash galery plugin, and caugth: Uncaught TypeError: Object [object Object] has no method 'accordion' - like it was before :( This plugin initialized his jquery and mine not works :( – BLiN Nov 16 '12 at 16:42
  • But I do know that thay can works together, because id i use jquery UI plugin, it works, but not correctly :( through http://stackoverflow.com/questions/1542161/jquery-ui-accordions-within-tabs – BLiN Nov 16 '12 at 16:48
  • I see the problem - jQuery is still included after your scripts and therefore it's overriding it. I'll look into that when I get back home after an hour or so. – Nikola Ivanov Nikolov Nov 16 '12 at 16:48
  • Yes, this line appears when I turn on the plug-in flash gallery - - If I understand it is built-in wordpress jquery. May be I need enable built-in support for tabs and accordions and use it? Somesing like wp_enqueue_script( 'jquery-ui-accordion' );? – BLiN Nov 16 '12 at 16:59