Support » Fixing WordPress » all from all categories except for one where I want only 5..

  • 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 15 replies - 1 through 15 (of 18 total)
  • Very difficult to be sure, but I think this might work. Change this:

    if ( $categories ) {
       $cat_slug = $categories[0]->slug;
    }

    to this:

    if ( $categories ) {
       $cat_slug = $categories[0]->slug;
    }
    if ( $cat_slug == 'blogg' && ++$blogg_count > 5 ) continue;
    Thread Starter klarabara

    (@klarabara)

    Hm… something is up, I get this error.

    Parse error: syntax error, unexpected ‘&’…www/wp-content/themes/amazing_memory/functions.php on line 110

    The # makes my text-editor recognize the rest as a comment, is there some qoutation-marks missing somewhere?

    Not sure where the # is coming from; there is no # in the code I posted. Please post the 4 lines of code from your functions.php.

    Thread Starter klarabara

    (@klarabara)

    Oh, I’m sorry. I pasted from the email that I got and it messed it up >.<

    Now I get five on the first page but just the same 5 when I filter on blogg. All the posts aren’t random anymore, maybe shuffle could fix that..

    I can’t tell from the code you posted how you would detect that a filter is in effect. Perhaps there is an $_GET variable named data-filter that has a value of ‘blogg’.

    If so, this might work:

    if ( $categories ) {
       $cat_slug = $categories[0]->slug;
    }
    $unfiltered = ( isset($_GET['data-filter']) && $_GET['data-filter'] == 'blogg' ) ? false : true;
    if ( $unfiltered && $cat_slug == 'blogg' && ++$blogg_count > 5 ) continue;
    Thread Starter klarabara

    (@klarabara)

    I tried doing similar last night, but as the link never change the sending a _get wouldnt work, but maybe there is a way around it?

    I tried this earlier today but I didnt manage to get the right values to be sent.

    http://foreverheavy.com/sort-wordpress-posts-by-category-without-refreshing-using-jquery/

    Looking at this line in your code seems to indicate that ‘data-filter’ is used in the link:

    <li> <a href="'.home_url().'">name.'" data-filter="' . $category->slug. '">' . $category->name.'</a> </li>

    Is that incorrect? What is the actual href?

    Thread Starter klarabara

    (@klarabara)

    Can you use this attribute to send a variable? Its used in the link but the href is still the same home_url()

    Could you explain?

    Please show me the actual href value.

    Thread Starter klarabara

    (@klarabara)

    This is what the href for blogg says:

    <a class="hidediv" data-filter="blogg" title="Blogg" href="http://mywebsite.se" style="color: rgb(0, 255, 0);">Blogg</a>

    For some reason, the code does not seem to agree with that href value. The code does not show ‘class=…’, ‘style=…’ anywhere. Are you certain that you posted the correct code?

    Thread Starter klarabara

    (@klarabara)

    Hm, you are actually right. I must have added that since I posted, sorry.

    This is the output. The class “showdiv” is used to slide-down a div.
    The style is just color that this script adds to the current menu-item:

    //currentlink
    $(document).ready(function(){
    var $container = $('#container');
    var toFilter = '*';
    	$container.isotope({
    		filter: toFilter,
    		animationOptions: {
    		duration: 750,
    		easing: 'linear',
    		queue: false,
    		}
    	});
    	$container.attr('data-current',toFilter);
    	checkActive();
    
    	$('#filters a').click(function(){
    		var title = $(this).attr('data-filter');
    		var text = $(this).text();
    		if(text == "Visa allt"){
    			var selector = title;
    			}
    			else {
    			var selector = "." + title;
    			}
    		$container.attr('data-current',selector);
    		$container.isotope({
    			filter: selector,
    			animationOptions: {
    				duration: 750,
    				easing: 'linear',
    				queue: false,
    			}
    		});
    		checkActive();
    		return false;
    	});
    		function checkActive(){
    		$('#filters a').each(function(){
    			$(this).css({
    				color: '#333333'
    
    			});
    				var title = $(this).attr('data-filter');
    				title = '.'+title;
    				if(title=='.*'){
    					title = '*';
    				}
    			var currentCat = $container.attr('data-current');
    			if(title==currentCat){
    				$(this).css({
    				color: '#00ff00'
    			});
    			}
    		});
    		}
    	});

    This is how the menu is outputted:

    foreach($categories as $category) {
    	    echo '<li> <a href="'.home_url().'" class="hidediv" title="'.$category->name.'" data-filter="' . $category->slug. '">' . $category->name.'</a> </li> ';
    	    }
    Thread Starter klarabara

    (@klarabara)

    Ive tried to combine in every which way with these scripts without reaching a conclusion..

    Thread Starter klarabara

    (@klarabara)

    if ( $categories ) {
       $cat_slug = $categories[0]->slug;
    }
    $unfiltered = ( isset($_GET['data-filter']) && $_GET['data-filter'] == 'blogg' ) ? false : true;
    if ( $unfiltered && $cat_slug == 'blogg' && ++$blogg_count > 5 ) continue;

    the $blogg_count, where is it getting its value?

    I am not proficient in Java, so I can’t be sure this will work, but try adding a parameter to the href to show the filter:

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

    and then check for $_GET[‘filter’] == ‘blogg’:

    if ( $categories ) {
       $cat_slug = $categories[0]->slug;
    }
    $unfiltered = ( isset($_GET['filter']) && $_GET['filter'] == 'blogg' ) ? false : true;
    if ( $unfiltered && $cat_slug == 'blogg' && ++$blogg_count > 5 ) continue;
Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘all from all categories except for one where I want only 5..’ is closed to new replies.