• I’m working on a site where its all based on categories, all posts are shown on index and are filtered with jquery. Works wonders! Just that one category are normal blogposts and I dont want the content to drown in blogposts.

    So my question is:
    Is there a way to show only 5 blogposts from the category ‘blogg’ and all the posts from the other categories? And then when filtering on ‘blogg’ see all blogg-posts, not just 5?

    The code I have for now is:

    $args = array( 'numberposts' => -1, 'orderby' => 'rand');
    
    		$rand_posts = get_posts( $args );
    		foreach ( $rand_posts as $thumbnail ) {
          		if ( has_post_thumbnail( $thumbnail->ID ) ) {
                  $categories = get_the_category( $thumbnail->ID );
                  $cat_slug = "";
                  if ( $categories ) {
                  	$cat_slug = $categories[0]->slug;
                  }
    
    			$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($thumbnail->ID), 'thumbnail');
    			$placeholder = get_bloginfo('stylesheet_directory')."/assets/placeholder.gif";
    			$thumb = get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' ); 
    
    			echo '<a class="showdiv"  href="' . get_permalink( $thumbnail->ID ) . '" title="'.esc_attr( $thumbnail->post_title ).'">';
                echo ' <div class="'. $cat_slug. ' element">';
                // echo get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
    			echo "<img class='lazy' data-original='$large_image_url[0]' src='$placeholder'>";
                echo "</div>";
               	echo "</a>";
            }
    }

    And the menu with filtering:

    <?php
    	$args=array(
    	  'orderby' => 'term_group',
    	  'order' => 'DESC'
    	  );
    	$categories=get_categories($args);
    	echo '<ul id="filters">';
    	echo '
    <li> <a href="'.home_url().'" title="Allt">Visa allt</a> </li>
     ';
    	  foreach($categories as $category) {
    	    echo '
    <li> <a href="'.home_url().'">name.'" data-filter="' . $category->slug. '">' . $category->name.'</a> </li>
     ';
    	    }
    	echo '';
    	?>

    Very very grateful for any suggestion!

Viewing 3 replies - 16 through 18 (of 18 total)
  • Thread Starter klarabara

    (@klarabara)

    I wrote the code like this:

    echo '<li> <a href="'.home_url().'/?filter='.$category->slug.'" class="hidediv" title="'.$category->name.'" data-filter="' . $category->slug. '">' . $category->name.'</a> </li> ';

    And now I can put the url in the adress-field and get _all posts_ and if I after that press the filter-button and it only shows the blogg-posts. It doesnt work by clicking the filter-button blogg directly. Is it that it needs to send a new query to the database?

    The url
    https://mywebsite.se/?filter=blogg

    As I said, I am not proficient in Java, so I am afraid I can’t answer that question.

    Thread Starter klarabara

    (@klarabara)

    Bummer.. I will see if I can get the jquery to fetch the info, thanks!

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘all from all categories except for one where I want only 5..’ is closed to new replies.