WordPress.org

Ready to get started?Download WordPress

Forums

display only one category on a post tagged with multiple categories (4 posts)

  1. kristinachilds
    Member
    Posted 2 years ago #

    i've got the newest post from each category displaying on the homepage. only there is one category that will always be tagged with two. Category 6 will always be also tagged with cat 12, however cat 12 will not always be tagged with 6.

    on the homepage, the post in category 6/12 shows both category names. i want it to display only the category 6 name.

    anyone know how to do this? here is the current loop

    <!-- LATEST NEWS -->
    	<?php
    		$post_ids = array(0);
    		foreach( array(1,2,4,5,6,7,8,9,10,11,13,14,15,16,17) as $cat_id ) {
    			if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => 15)) ) {
    				$first = array_shift($posts);
    				$post_ids[] = $first->ID;
    			}
    		}
    		query_posts(array('post__in' => $post_ids));
    	?>
    	<?php while (have_posts()) : the_post(); ?>
    
    	<div class="latest-post-wrap">
            <div class="latest-post">
                <div class="latest-title">
                    <h2><?php the_category(' / ') ?></h2>
                </div>
                <div class="latest-image">
                    <a href="<?php the_permalink() ?>">
                        <?php if ( get_post_meta($post->ID, 'image', true) ) : ?>
                            <img src="<?php bloginfo('template_url'); ?>/mainimagecrop.php?w=162&h=118&f=<?php echo get_post_meta($post->ID, 'image', true) ?>" />
                         <?php endif; ?>
                    </a>
                </div>
            </div>
            <div class="latest-content">
                <span style="font-size:14px; color:#5CB7CC;font-weight:bold;text-transform:uppercase;"><br /><?php the_title(); ?></span><br />
                <div class="postMeta-front">
                    <span class="date"><?php the_time('F j, Y') ?></span>
                    <span class="comments"><?php comments_popup_link('0', '1', '%'); ?></span>
                </div>
                <div class="clear"></div>
                <p>
                    <?php
                    $excerpt = get_the_excerpt();
                    echo string_limit_words($excerpt,30);
                    ?>
                </p>
                <p class="moretext"><a href="<?php the_permalink() ?>">Continue Reading...</a></p>
            </div>
        </div>
    
    	<?php endwhile; ?>
        <!-- LATEST NEWS END -->
  2. kristinachilds
    Member
    Posted 2 years ago #

    i tried using get_the_category, and that omits the 2nd category name from the homepage display, however it also omits any other post in category 12.

    <?php
    $category = get_the_category();
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    }
    ?>

    it would be great if there could be something like

    "if category 6 and 12, then only display category name 6"

    i don't know how to write php directly, only hack it using functions already written so need a little help from actual php developers

  3. kristinachilds
    Member
    Posted 2 years ago #

    something like this (but that works)

    <?php
       $category = get_the_category();
       if ($category[6]) {
          echo '<?php get_category_link( $category_id ); ?>' ;
       } else ($category[0]) {
          echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
       }
    ?>
  4. kristinachilds
    Member
    Posted 2 years ago #

    apparently i need some more coffee because get_the_category actually works. i just neglected to put '12' in the array of categories to loop.

    awesome.

Topic Closed

This topic has been closed to new replies.

About this Topic