ThemeShaper Forums » Thematic

[closed]

$post->ID not working in thematic_abovemainasides

(7 posts)
  • Started 12 years ago by chrizbot
  • Latest reply from jonnycj
  • This topic is resolved

Tags:

  1. chrizbot
    Member

    Hi.

    I am trying to do a custom sub-menu in the sidebar. For some reason no matter what I do, I can't seem to get $post->ID or $post->post_parent to return anything.

    Here is a simplified version of my function being called for thematic_abovemainasides (0):

    function childtheme_sidebar_menu() {
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
    if ($children) { ?>
    <div id="children-page-list">
    <?php echo $children; ?>
    </div>
    <?php }
    }

    Only the top level menu items are shown as it never actually gets $post->ID.

    I also tried to get the page ID by using $wp_query->post->ID, but that also returns nothing.

    What am I doing wrong here?

    Thanks!

    Posted 12 years ago #
  2. first you must call global $post; if you want using $post->ID

    function childtheme_sidebar_menu() {
    global $post;

    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
    if ($children) { ?>

    <div id="children-page-list">
    <?php echo $children; ?>
    </div>
    <?php }
    }

    Posted 12 years ago #
  3. chrizbot
    Member

    Awesome, that is what I was missing. My PHP is pretty rusty. Thanks!

    Posted 12 years ago #
  4. jonnycj
    Member

    I have a related query on global $post;

    If I am using the following code inside a page template to grab specific content from a post then global $post; fails to work or identify the page that the template relates to and hence my sidebar menu displays nothing

    The code for grabbing the post is as follows:

    <?php
        // adding in the query for posts
    			$wp_query = null; ?>
    			    <?php $wp_query = new WP_Query('category_name=survivorsnews&posts_per_page=1'); ?>
    			    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    
                    <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
    				<?php the_excerpt(); ?><a href="<?php the_permalink(); ?>" class="more"><?php echo more_text() ?></a>
    </a>
    	    		<?php endwhile; ?>

    The code for creating my sidebar is similar to that chrizbot uses but is as follows:

    <?php
    global $post;
    ?>
    <?php
      if($post->post_parent) {
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      $titlenamer = get_the_title($post->post_parent);
      }
    
      else {
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      $titlenamer = get_the_title($post->ID);
      }
      if ($children) { ?>

    Any thoughts on how I can get this to work ?

    Posted 12 years ago #
  5. a new query messes up the old query, so your $post variable will be different i think.

    try using get_posts instead

    http://codex.wordpress.org/Template_Tags/get_posts

    Posted 12 years ago #
  6. jonnycj
    Member

    Thanks Helga, I have been using a new query instead of get_posts as this helps me to use pagination ($paged) properly.

    I suppose what I am asking is, assuming that I have created a new query is there a way for my sidebar code to restore the old one so that the sidebar code can work out what page it is on i.e. what do I need to do so that when the sidebar calls:

    <?php
    global $post;
    ?>

    There is a content to that variable and therefore my page list can be created ?

    Posted 12 years ago #
  7. jonnycj
    Member

    I have now resolved this using the following code in conjunction with my standard sidebar menu code above. Thanks to Middlesister for this. This is the code inside my page template

    <?php
    // adding in the query for posts
    $lum_store_wp_query = $wp_query;
    $wp_query = null; ?>
    <?php $wp_query = new WP_Query('category_name=survivorsnews&posts_per_page=5&paged='.$paged ); ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
    <a href="<?php the_permalink() ?>"></a>
    <h3 class="entry-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    <?php the_excerpt(); ?><a href="<?php the_permalink(); ?>" class="more"><?php echo more_text() ?></a><br /><br />
    <?php thematic_postfooter(); ?>
    </div><!-- .post -->
    <?php endwhile; ?>
    <?php	thematic_nav_below(); ?>
    <?php
    $wp_query = null;
    $wp_query = $lum_store_wp_query;
    wp_reset_postdata();
    ?>

    This query format allows pagination of posts inside a page template to work along with a context sensitive sidebar menu. Code as follows for sidebar menu (inserted into the primary aside widget area) :

    <?php
    global $post;
    ?>
    <?php
    if($post->post_parent) {
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    $titlenamer = get_the_title($post->post_parent);
    }
    
    else {
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    $titlenamer = get_the_title($post->ID);
    }
    if ($children) { ?>
    <h1> <?php echo $titlenamer; ?> </h1>
    <ul>
    <?php echo $children; ?>
    </ul>
    
    <?php } ?>
    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.