• Hi!

    I have found a plugin called Category show that I would like to implement in my archive.php page.

    When clicking an archive link in my menu I would like to generate the code for my plugin.

    I guess it’s best to add an if-loop to my archive.php page.

    I want something like this:

    If category = 9 then

    My code here for category 9

    elseif category = 212 then

    My code here for category 212

    else

    The usual code that is in archive.php

    end if

    The code in archive.php looks like this:

    <?php get_header(); ?>
    
    <div id="content">
    
    	<div id="column">
    
    	<?php if (have_posts()) : ?>
    	<div class="post_header">
    	<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
    	<?php /* If this is a category archive */ if (is_category()) { ?>
    	<h1>Arkivet för kategorin ‘<?php single_cat_title(); ?>’</h1>
    	<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    	<h1>Inlägg taggade med ‘<?php single_tag_title(); ?>’</h1>
    	<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
    	<h1>Arkivet för <?php the_time('F jS, Y'); ?></h1>
    	<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
    	<h1>Arkivet för <?php the_time('F, Y'); ?></h1>
    	<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
    	<h1>Arkivet för <?php the_time('Y'); ?></h1>
    	<?php /* If this is an author archive */ } elseif (is_author()) { ?>
    	<h1>Författararkiv</h1>
    	<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
    	<h1>Arkiv</h1>
    	<?php } ?>
    	</div>
    
    	<?php while (have_posts()) : the_post(); ?>
    	<div class="post">
    		<h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    		<div class="postinfo">
    		<div class="info">Skrivet av <?php the_author_posts_link(); ?> on <?php the_time('j F, Y'); ?></div>
    		<div class="commentnum"><?php comments_popup_link('Inga kommentarer än', '1 kommentar', '% kommentarer'); ?></div><div class="clear"></div>
    <?php if(function_exists('the_views')) { the_views(); } ?>
    <?php if(function_exists('the_ratings')) { the_ratings(); } ?>
    		</div>
    		<div class="category">Inlägget tillhör kategori: [ <?php the_category(', '); ?> ]</div>
    		<div class="entry">
    
    		<?php the_excerpt(); ?><div class="clear"></div>
    		<p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="more-link">Läs hela inlägget...</a></p>
    
    		</div>
    		<?php the_tags('<div class="tags">Taggar: [ ', ', ', ' ]</div><div class="clear"></div>'); ?>
    	</div>
    	<?php endwhile; ?>
    
    	<!-- Plugin Navigation -->
    	<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    	<!-- End -->
    
    	<?php else : ?>
    	<div class="post">
    	<h1>No posts were found.</h1>
    	<p>Sorry! the page you are looking for does not exist.</p>
    	<h3>Blog Search</h3>
    	<?php include(TEMPLATEPATH."/searchform.php"); ?>
    	</div>
    	<?php endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    
    <div class="clear"></div>
    </div>
    
    <?php get_footer(); ?>

    Anybody able to help me out?

Viewing 6 replies - 1 through 6 (of 6 total)
  • To test if the archive being display is that of category 9 you would use is_category(9)

    To test if category 9 is assigned to a post you would use in_category(9).

    See Conditional Tags.

    <?php
    if (is_category(9)) {
      echo 'this is a category 9 list of posts';
    } else {
    if (is_category(7)) {
      echo 'this is a category 7 list of posts';
    } else {
      echo 'this is not a category 9 or 7 list of posts';
    }
    ?>

    Thread Starter webkocken

    (@webkocken)

    Thanks for answering!

    I get this message when using your code:
    Parse error: syntax error, unexpected T_ELSE in /home/web79400l/domains/designadinblogg.se/public_html/wp-content/themes/true-elegance/archive.php on line 60

    … meaning what?

    Thread Starter webkocken

    (@webkocken)

    I got it working with this code:

    <?php
    if (is_category(9)) {
        echo "Category 9";
    } elseif (is_category(7)) {
        echo "Category 7";
    } else {
        echo "Other stuff";
    }
    ?>

    Hopefully I can get it all to work for me now.

    Thanks for the help!

    Thread Starter webkocken

    (@webkocken)

    Hm, I ran into a problem right away.

    The code that I use for the plugin [Category Show] is this:

    %%wpcs-9%%-date%%id%%

    But that wont work in the archive.php file.

    How can I make it work directly in my archive.php file?

    I tagged this thread with the ‘wp-catergory-show’ tag so the plugin author might see it.

    Sorry for the delay.

    The code for the category show plugin only works on a page post.

    []’s

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Create an if loop in archive.php’ is closed to new replies.