2

I loaded wordpress page titles in top of my theme using get_pages() function like a navigation menu:

<ul>
<?php $pages = get_pages('child_of=0&parent=0&echo=0'); 

   foreach($pages as $page){
     echo '<li class=pages id=menu_'.$page->ID.' >';
     echo $page->title;
     echo '</li>';
   }
?>
</ul>

Note:

  1. that the printed page titles doesn't have link to the page.
  2. and all <li> tags have same class=pages
  3. and each <li> tag has an id related to current $page->ID

and I've used Jquery for call a function to show children of each page on click of each <li> tag with 2 last character of id attribute like below:

<script type=text/javascript>
  $(document).ready(function(){
    $('.pages').click(function(){
      var id = $(this).attr('id');
      id = id.substring(5,7);
      $('body').append(<?php get_pages('child_of=id&echo=1'); ?>);
    });
  });
</script>

Bu It doesn't work, it seems the PHP block code called when page loaded. Please help me to call the wordpress get_pages function on click event of each <li> to load children of pages.

2
  • you are trying to call a php function in javascript? Commented Dec 13, 2011 at 10:04
  • @Shvelo Yes, but I think I tried a wrong way, what's the right answer? Please do this example with ajax. I didn't use ajax at all! Commented Dec 13, 2011 at 10:07

3 Answers 3

0

Nobody is going to do this for you, but here are a few resources that may help you figure it out and lead you down the right path:

Intro to AJAX: http://www.w3schools.com/Ajax/ajax_intro.asp

Intro to AJAX (video): http://www.youtube.com/watch?v=tJXLRLDWjn4

Everything you need to know about AJAX and how to use it with WordPress: http://codex.wordpress.org/AJAX Hint: follow the links under the "Developer Information" section.

Sign up to request clarification or add additional context in comments.

Comments

0

PHP runs on the server. Javascript/jquery runs in the browser. You can't directly run PHP from your javascript.

What you need to do is learn about ajax generally. Then learn about ajax and wordpress.

Then you can use jquery to fetch data from whatever APIs wordpress exposes to fetch the information you want, and display that data to the user.

Comments

0

Why don't you simply load everything on the loop (a new foreach for subpages inside the pages' foreach loop) and then use your jQuery function (modified) to toggle the subpages on click?

1 Comment

Good plan. Could you provide an example for the OP to improve your answer? What you have written is more of a 'comment'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.