dantheman
dantheman

Reputation: 273

Change class of every second instance of a div?

I am working on a Wordpress site and I want to create an unordered list that has categories. The categories consist of subjects such as : Math, English, Science, etc. A user can upload a file related to one of these categories and give a brief description of the file. However I don't want the categories to repeat themselves for every instance ex:

-English
Hamlet by Shakespeare
Read 1 chapter every week and take notes

-English
Reading Assignment
Read all 12 questions and answer in full sentences.

Instead I would like the first instance of this category to appear and have other posts that are of the same category to be appended to the first instance. example:

-English

-Hamlet by Shakespeare
Read 1 chapter every week and take notes

-Reading Assignment
Read all 12 questions and answer in full sentences.

This is what I have so far:

cat = document.getElementsByClassName("catname"); 

for(var i =0; i < cat.length ; i++){
  if (cat[i].innerHTML == "General"){
    cat[i].className = "hide";
   }
  else if(cat[i].innerHTML == "English"){
    cat[i].className = "hide"; 
   }
  else if(cat[i].innerHTML == "French"){
    cat[i].className = "hide"; 
   }
  else if(cat[i].innerHTML == "Math"){
    cat[i].className = "hide"; 
   }
  else if(cat[i].innerHTML == "Science"){
   cat[i].className = "hide";
  }
}

Any help would be much appreciated! Thanks!

Upvotes: 0

Views: 73

Answers (1)

ASGM
ASGM

Reputation: 11381

Rather than manipulating the category labels after the page has been generated, why not store the categories explicitly in the backend? Make each subject a Wordpress category, then display lists of the posts by category. You could do that using a plugin or with your own PHP code in the template (based on this post):

<?php $cats = get_categories(); 
foreach ($cats as $cat) {
    $cat_id= $cat->term_id;
    echo "<h2>".$cat->name."</h2>";
    query_posts("cat=$cat_id&posts_per_page=100");
    if (have_posts()) : while (have_posts()) : the_post(); ?>
        <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
    <?php endwhile; endif; ?>
<?php } ?>

Upvotes: 1

Related Questions