• I have been using the following code in Shortcode Exec PHP to generate a context-sensitive menu which displays the ancestry of the current page, plus with its immediate siblings and children. Having imported this, the code looks identical: but the menu now displays every single page in the family tree, making it impracticably long! So I’ve had to revert to Shortcode Exec PHP for now.

    I’m guessing this may be something to do with precisely when the code is being executed: but I can’t see any options for controlling this. Is there a simple fix?

    
    //CONTEXT MENU code - defined as 'context_menu' in Shortcode Exec PHP
    //Invoked as [context_menu] in Sidebar Right text widget
    
    //if the post has no parent - it's a top-level page
    if(!$post->post_parent){
      $relations = array(); // no ancestors
      //Search the database for any child and other top-level pages
      global $wpdb;
      $result = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE (post_type='page' OR post_type='post') AND (post_parent = $post->ID OR post_parent = 0)" );
    
    }else{
      //Tricky - First, request array of ancestor pages
      $relations = get_post_ancestors($post->ID);
      //Now search the database for any child and sibling pages
      global $wpdb;
      $result = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE (post_type='page' OR post_type='post') AND (post_parent = $post->ID OR post_parent = $post->post_parent)" );
    }
    
    if ($result){
      //Add any finds to the array
      foreach($result as $pageID){
        array_push($relations, $pageID->ID);
      }
    }
    //Reduce array to a comma delimited list
    $relations_string = implode(",",$relations);
    //Now request a list of the collected pages, wrapped in <li> tags 
    $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string);
    if ($sidelinks) { ?>
      <ul id="ContextMenu">
        <? //Not an empty list, so wrap it in <ul> tags
        echo $sidelinks; ?>
      </ul>         
    <? }
    
    • This topic was modified 7 years, 1 month ago by liegeman. Reason: Used wrong tags to enclose code

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter liegeman

    (@liegeman)

    Sorry – I initially used b-quote instead of code tags. The code shown above is now correct: but the problem is as previously described.

    • This reply was modified 7 years, 1 month ago by liegeman.
    Thread Starter liegeman

    (@liegeman)

    I’ve now managed to work around my problem by using the Advanced Sidebar Menu plugin to insert a widget with comparable functionality in place of my ‘Context Menu’ shortcode. I’m de-installing Shortcode Exec PHP, but retaining Add Shortcodes Actions And Filters for possible future use; so would still be interested if anyone can offer any insight as to what may have caused the above incompatibility and how to fix it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Timing problem with code imported from Shortcode-exec-PHP’ is closed to new replies.