WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Widget Logic] widget logic an is_page_template('category-1.php') (9 posts)

  1. MaHeHe
    Member
    Posted 5 years ago #

    Hi,

    I use category-1.php, category-2.php ... for Category Pages.
    Now I want to use is_page_template('category-1.php') as conditional tag with widget logic to control the appearance of a widget, but it doesn't work.

    What's the problem and what's the solution?
    Thanks for help.

  2. alanft
    Member
    Posted 5 years ago #

    category-1.php etc aren't page templates. what you have there are just regular parts of the WP hierarchy - in this case to show the list of recent posts in category 1 etc. page templates are php files used when creating individual pages.

    so you should find that on those pages, when just showing category 1 etc, you can use the ordiinary is_category('1') style conditional tags

    hope this helps

  3. MaHeHe
    Member
    Posted 5 years ago #

    Thanks for answering.

    Before trying with is_page_template, I tried is_category too.
    Unfortunately it didn't work.

    My category-5.php, for example, is similiar to and based on Mimbos index.php.
    This is my category-5.php. Categories 9-17 are sub-categories of category 5:
    Mimbos lead-story is a static page in my category-5.php...

    <?php get_header(); ?>
    <div id="content">
        <?php /* If this is a category archive */ if (is_category()) { ?>
    		<h2 class="pagetitle"><?php single_cat_title(); ?></h2> 	  <?php } ?>
      <div class="feature clearfloat" id="lead"> 	
    
       <?php
    // this is where category-page begins
       query_posts('showposts=1&page_id=102'); ?>
        <?php while (have_posts()) : the_post(); ?>
    
        <div align="justify"><?php
    // this is where the content of category-page gets printed
    	the_content(); ?></div>
            <?php endwhile; ?>
      </div><!--END CATEGORY-PAGE-->
    
      <div id="thirdcol">
    <?php
    // this is where you enter the IDs of which categories you want to display
    $display_categories = array(9,11,13,15,17);
    foreach ($display_categories as $category) { ?>
        <div class="clearfloat">
          <?php query_posts("showposts=1&cat=$category");
    	    $wp_query->is_category = false;
    		$wp_query->is_archive = false;
    		$wp_query->is_home = true;
    		 ?>
          <h3><a href="<?php echo get_category_link($category);?>"><?php
    	// this is where the name of each category gets printed
    	  single_cat_title(); ?></a></h3>
          <?php while (have_posts()) : the_post(); ?>
          <?php
    // this grabs the image filename
    	$values = get_post_custom_values("Image");
    // this checks to see if an image file exists
    	if (isset($values[0])) {
    ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" /></a>
          <?php } ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php
    // this is where title of the article gets printed
    	  the_title(); ?>&raquo;</a>
          <div align="justify"><?php the_excerpt(); ?></div>
          <?php endwhile; ?>
        </div>
        <?php } ?>
      </div><!--END LEFTCOL-->
    
      <div id="rightcol">
        <?php
    // this is where you enter the IDs of which categories you want to display
    $display_categories = array(10,12,14,16);
    foreach ($display_categories as $category) { ?>
        <div class="clearfloat">
          <?php query_posts("showposts=1&cat=$category");
    	    $wp_query->is_category = false;
    		$wp_query->is_archive = false;
    		$wp_query->is_home = true;
    		 ?>
          <h3><a href="<?php echo get_category_link($category);?>"><?php
    	// this is where the name of each category gets printed
    	  single_cat_title(); ?></a></h3>
          <?php while (have_posts()) : the_post(); ?>
          <?php
    // this grabs the image filename
    	$values = get_post_custom_values("Image");
    // this checks to see if an image file exists
    	if (isset($values[0])) {
    ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_url'); ?>/images/<?php $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt="" /></a>
          <?php } ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php
    // this is where title of the article gets printed
    	  the_title(); ?>&raquo;</a>
          <div align="justify"><?php the_excerpt(); ?></div>
          <?php endwhile; ?>
        </div>
        <?php } ?>
      </div><!--END RIGHTCOL-->
    </div><!--END CONTENT-->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    So, is there any possibility for me to implement my wishes? (On an easy, newbie-way...)
    I wish to display a category-widget for categories 9-11 (with "Extended Categories") while displaying category-5.php (and category-9.php...).

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Heh. Sorry, we ninja edited over each other here. :)

    Add this right before your call to get_sidebar at the bottom of the page:
    <?php wp_reset_query(); ?>

    Then use the is_category(5) stuff in the way you're expecting to use it.

  5. MaHeHe
    Member
    Posted 5 years ago #

    Thanks a lot!
    Problem resolved!

  6. alanft
    Member
    Posted 5 years ago #

    if what you want is logic to show when it's category 5, 9,10,11...16 or 17 you CAN use code like

    is_category(array(9,10,11,12,13,14,15,16,17))

    see http://codex.wordpress.org/Conditional_Tags#A_Category_Page

    HOWEVER, it's good that you posted the code of your category-1.php, cos that's complicated and unusual in that it's a sort of 'fake' category page running multiple loops and calling query_posts multiple times and modifying $wp_query, which the usual conditional tags depend on.

    the sidebar is called after all those so the widget logic you can use will be limited by the last query_posts run and $wp_query modifications made. for whatever reason your code leaves wp_query set with 'wp_query->is_home = true;' making it seem indistinguishable from the home page as far as WP's conditional tags will be able to tell.

    if you still want widget logic to work on things like is_category('5') you'd have to 'save' the original wp_query before running all this code and restoring it before the sidebar is asked for. you may need to google around for 'multiple loop' info on how to do that.

    BUT a simpler approach, given the complexity of the code already, might be just to specify a new global variable you can test in the widget logic. so before get_sidebar(); add something like

    $my_fake_category=5;

    and then your widget logic could very simply be:

    ($my_fake_category==5)

    i haven't tried this, but it seems good. you may need the more complex

    global $my_fake_category; return ($my_fake_category==5);

    should the global variable not be 'visible' to the widget logic code.

  7. alanft
    Member
    Posted 5 years ago #

    x-post. WHAT OTTO SAID! thanks guy

    (wp_reset_query is news to me!)

  8. MaHeHe
    Member
    Posted 5 years ago #

    Hi,

    is_category(array(....)) is exactly the tag I'm using now...

    As I said, my code is from the Mimbo-theme an it's based on index.php, that's why is_home = true is the last.
    I didn't really know, what's all the stuff in "my" code about, but with your comment a lot of things became clear.

    So, thanks for your help!

  9. TradingMetro
    Member
    Posted 5 years ago #

    Thank you for this post! This solved a few problems!
    wp_reset_query(); was needed.

    I think, for the future, I'll modify query_posts() to WP_Query.

Topic Closed

This topic has been closed to new replies.

About this Topic