WordPress.org

Ready to get started?Download WordPress

Forums

all from all categories except for one where I want only 5.. (19 posts)

  1. klarabara
    Member
    Posted 1 year ago #

    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!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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;
  3. klarabara
    Member
    Posted 1 year ago #

    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?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  5. klarabara
    Member
    Posted 1 year ago #

    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..

  6. vtxyzzy
    Member
    Posted 1 year ago #

    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;
  7. klarabara
    Member
    Posted 1 year ago #

    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/

  8. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  9. klarabara
    Member
    Posted 1 year ago #

    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?

  10. vtxyzzy
    Member
    Posted 1 year ago #

    Please show me the actual href value.

  11. klarabara
    Member
    Posted 1 year ago #

    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>

  12. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  13. klarabara
    Member
    Posted 1 year ago #

    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> ';
    	    }
  14. klarabara
    Member
    Posted 1 year ago #

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

  15. klarabara
    Member
    Posted 1 year ago #

    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?

  16. vtxyzzy
    Member
    Posted 1 year ago #

    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;
  17. klarabara
    Member
    Posted 1 year ago #

    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
    http://mywebsite.se/?filter=blogg

  18. vtxyzzy
    Member
    Posted 1 year ago #

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

  19. klarabara
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags