WordPress.org

Ready to get started?Download WordPress

Forums

PHP Code Widget
[resolved] Listing subpages not working (2 posts)

  1. mike62
    Member
    Posted 3 years ago #

    The following code, when pasted into the template sidebar, works perfectly, but doesn't work when using this plugin.

    <?php
    //show parent page
    $parent_title = get_the_title($post->post_parent);?>
    <a href="<?php echo get_permalink($post->post_parent) ?>" class="parent"><?php echo $parent_title;?></a>
    
    <?php
    //show child pages
      if($post->post_parent)
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <ul id="subnav">
      <?php echo $children; ?>
      </ul>
      <br clear="all" />
    <?php } ?>
  2. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 3 years ago #

    You don't have $post defined.

    The contents of the widget do not run in the global context like templates do. If you want access to global variables, you have to declare them.

    In your case, adding global $post; to the beginning of your code would probably work.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic