ThemeShaper Forums » Thematic

[closed]

is_page() and custom template pages

(8 posts)
  • Started 2 years ago by jjcorradi
  • Latest reply from jonnycj
  • This topic is resolved
  1. jjcorradi
    Member

    Hi everybody,
    i'm developing a child theme and - in the functions.php of the child theme - i'm using an action hook to put a menu in the thematic sidebar; i want the menu to be displayed only in a certain page (page_id = 12).
    Everything works good if the page uses a default template, but if I define a custom template and I assign it to this page I don't see the menu anymore. (The custom template is identical to the default one except for a few lines that don't affect the sidebar).

    Can you help me to figure out why does it happen and to solve the problem?
    Thanks a lot
    andrea

    This is the function:

    <?php
    function studioIQ_sidebar_profilo() { ?>

    <?php if (is_page(12)) { ?>

    <div>

    • ...
    • ...

    </div>

    <?php }
    }

    add_action('thematic_sidebar','studioIQ_sidebar_profilo');

    ?>

    Posted 2 years ago #
  2. jjcorradi
    Member

    The problem is the code I added in the custom template page:

    <?php $temp_query = $wp_query; ?>
    <?php query_posts('category_name=profilo&showposts=5'); ?>

    <?php while (have_posts()) : the_post();?>

    <div id="post-<?php the_ID();?>" style="visibility:hidden; position: absolute; ">
    <?php the_content(); ?>
    <?php the_title(); ?>
    </div>

    <?php endwhile; ?>

    This is because calling the query_posts WordPress ignores the other parameters it receives via the URL (such as page number or category) [see http://codex.wordpress.org/Function_Reference/query_posts].

    If I try to do as they suggest in the page link I reported above:

    <?php global $query_string;
    query_posts( $query_string . '&category_name=profilo&showposts=5' ); ?>

    I don't obtain the posts filtered as I wanted (category_name=profilo).

    Anyone can help?

    Posted 2 years ago #
  3. maybe try just "cat=profilo"

    category_name seems to be for when you use an array

    Posted 2 years ago #
  4. middlesister
    Member

    Like you said, calling query_posts affects global values and the codex specifically states that it affects conditionals. Try the loop using a new WP_Query object instead.

    <?php $my_query = new WP_Query('category_name=profilo&posts_per_page=5'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post();?>
    
    <div id="post-<?php the_ID();?>" style="visibility:hidden; position: absolute; ">
    <?php the_content(); ?>
    <?php the_title(); ?>
    </div>
    
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    I am just starting to get my head around the difference between query_posts and WP_Query myself. It is not always clear what is the best practice. With your first code, you are storing the original query in a temporary variable, but not restoring it at the end. Does it work when you restore the query?

    <?php $temp_query = $wp_query; ?>
    <?php query_posts('category_name=profilo&posts_per_page=5'); ?>
    
    <?php while (have_posts()) : the_post();?>
    
    <div id="post-<?php the_ID();?>" style="visibility:hidden; position: absolute; ">
    <?php the_content(); ?>
    <?php the_title(); ?>
    </div>
    
    <?php endwhile; ?>
    <?php $wp_query = $temp_query; ?>
    Posted 2 years ago #
  5. jjcorradi
    Member

    Middlesister, I love you!
    Now it works fine :)

    Both ways are working, the new WP_query and the original query restore.
    Thanks a lot for your help!

    Posted 2 years ago #
  6. jonnycj
    Member

    Hello Middlesister,

    I am trying to get a similar idea running but also with pagination enabled so I can spread the posts across several pages. So far using a slight variation on your code (from here:http://codex.wordpress.org/Function_Reference/wp_reset_postdata) I get my sidebar menu working but pagination is still not active, so I wondered if you had any ideas.

    <?php
    // adding in the query for posts
    $original_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
    $wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();
    ?>

    Live page here:
    http://develop.survivorspoetry.org/?page_id=1347

    Any thoughts greatly appreciated

    Posted 2 years ago #
  7. middlesister
    Member

    I think this is because you are restoring the original query and using wp_reset_postdata straight after the loop, and the navigation for the pagination is usually included further down in the code. This means that the navigation would refer to the original query and not your custom loop. You do not specify how you are adding the loop or if you are making your own template. Make sure that there is a call for thematic_navigation_below() or the equivalent in the template, and add code for the resetting after the navigation. If you are using a thematic template, you could attach the resetting code to the thematic_belowcontent hook.

    Posted 2 years ago #
  8. jonnycj
    Member

    Exactly that ! I had sort of realised this and tried adding the reset_postdata into the sidebar but it was not having it.
    I have now added it directly after thematic_nav_below and voila. Thanks a lot for your input, you are a star !

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.