WordPress portföyünüze nasıl filtre eklenir?

Yayınlanan: 2016-03-21

Geçen ay size özel bir gönderi türü, yeni bir “projeler” sorgusu ve birkaç yeni şablon kullanarak WordPress'te nasıl portföy sitesi oluşturacağınızı gösterdim.

Bu yazımda size Isotope.js kütüphanesini kullanarak portföy sayfanıza nasıl filtre ekleyeceğinizi göstereceğim.

eklenti-filtreler-wordpress-portföy-tümü

Henüz yapmadıysanız, WordPress sitenize gidin, birkaç kategori oluşturun ve bunları portföyünüzdeki öğelere atayın. İlk makaleyi okursanız, yine 'Projeler' adında bir CPT kullanıyorum, bu nedenle WordPress siteme eklenen her proje için bir kategorinin atandığından emin olmam gerekecek. Sitemde projelerim “Yeni İnşaat” veya “Tadilat” olarak kategorize edilecek.

Projelerinizin her birine bir kategori atadıktan sonra, sizin için süslü filtreleme işini yapacak JavaScript'i eklemenin zamanı geldi.

Isotope.js, David DeSandro tarafından geliştirilen bir düzen kitaplığıdır. Açık kaynak ve kişisel kullanım için ücretsizdir, ancak ticari kullanım için bir lisans satın almanız gerekir. Daha fazla bilgi ve fiyatlandırma için lütfen lisans sayfasını inceleyin.

O halde isotope.js dosyasını sitenize ekleyelim. İdeal olarak, güncellemelerin asla silmemesini sağlamak için bunu alt /js/ adlı bir dizine eklersiniz.

Ayrıca bir dosya daha oluşturup /js/ dizininize eklemeniz gerekiyor. Bu dosya, projelerinizi hedeflemek için gereken jQuery'yi içerecektir. projects.js , bu dosyaya project.js adını verdim.

Aşağıdaki kodu bu dosyaya yapıştırın ve kaydedin. Bu dosyada dikkat edilmesi gereken en önemli şeyler #projects , .project-item ve #filters . Bu, layoutMode: duvarcılık, paketleme, cellByColumn ve daha fazlası olarak değiştirebileceğiniz dosyadır. Bu eğitimde ızgara modunu kullanıyorum.

jQuery(function ($) {

// initialize Isotope after all images have loaded

var $container = $('#projects').imagesLoaded( function() { //The ID for the list with all the blog posts

$container.isotope({ //Isotope options, 'item' matches the class in the PHP

itemSelector : '.project-item',

grid: {

columnWidth: 200

}

});

});

 
//Add the class selected to the item that is clicked, and remove from the others

var $optionSets = $('#filters'),

$optionLinks = $optionSets.find('a');
$optionLinks.click(function(){

var $this = $(this);

// don't proceed if already selected

if ( $this.hasClass('selected') ) {

return false;

}

var $optionSet = $this.parents('#filters');

$optionSets.find('.selected').removeClass('selected');

$this.addClass('selected');
//When an item is clicked, sort the items.

var selector = $(this).attr('data-filter');

$container.isotope({ filter: selector });
return false;

});

});

Bu kodu sitenize ekledikten sonra, dosyaları sıraya koymanın zamanı geldi. Eğer alt temanızın functions.php dosyasında bunu yapmak için önceden tanımlanmış bir fonksiyon varsa, ona aşağıdaki gibi bir kod satırı eklemeniz yeterli olacaktır.

wp_enqueue_script('isotope', get_stylesheet_directory_uri() . '/js/isotope.pkgd.min.js', array(), '1.0.0', true );

wp_enqueue_script('projects', get_stylesheet_directory_uri() . '/js/projects.js', array(), '1.0.0', true );

Not, get_stylesheet_directory_uri() her zaman mevcut aktif temaya atıfta bulunur.
Özel bir tema söz konusu olduğunda veya bir temayı alt tema olmadan değiştirdiğiniz senaryoda, büyük olasılıkla get_template_directory_uri() yerine get_stylesheet_directory_uri() kullanırsınız.
Alt temanız, betiğinizi kuyruğa almak için bir işleve sahip değilse, bir tane eklemeniz gerekir. isotope.js dosyanızı kuyruğa almak için aşağıdakileri kullanın.

add_action( 'wp_enqueue_scripts', 'child_scripts');

function child_scripts(){

wp_enqueue_script('isotope', get_stylesheet_directory_uri() . '/js/isotope.pkgd.min.js', array(), '1.0.0', true );

wp_enqueue_script('projects', get_stylesheet_directory_uri() . '/js/projects.js', array(), '1.0.0', true );

}

Burada son bir not, temanızın zaten jQuery'yi kuyruğa aldığından emin olmalısınız. Değilse, wp_enqueue_script('jquery'); eklemeniz gerekir. ancak WordPress 3.8'den itibaren jQuery, WordPress çekirdeğiyle /wp-includes/js/jquery/jquery.js içinde paketlenmiştir.

Son olarak, functions.php dosyanızı kaydedin.

Şimdi, projects-page.php dosyanıza geri dönün ve filtrelerinizi gösterecek kodu ekleyin, ayrıca kategori sayısını ve her projenin kategori adını alın.

Portföyünüzün başladığı yerin hemen üstüne aşağıdaki kodu yapıştırın. Son öğretici gibi, Bootstrap kullanıyorum ve aşağıdaki HTML'de filtre satırımı kabın tam genişliği olacak şekilde ayarlıyorum. Varsayılan olarak filtrelerim sütunun soluna hizalanır. Bootstrap kullanmıyorsanız, <ul> etiketleriyle başlayıp bitirmek isteyebilirsiniz.

&amp;amp;lt;div id=&quot;filter-row&quot; class=&quot;row&quot;&amp;amp;gt;
&amp;amp;lt;div id=&quot;project-page&quot; class=&quot;col-lg-12&quot;&amp;amp;gt;
		&amp;amp;lt;ul class=&quot;nav navbar-nav navbar-left&quot; id=&quot;filters&quot;&amp;amp;gt;
			&amp;amp;lt;?php
				$terms2 = get_terms(&quot;project_categories&quot;); // This will go get all the categories
				$count = count($terms2); //This counts the number of categories
				echo '&amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;a href=&quot;javascript:void(0)&quot; title=&quot;&quot; data-filter=&quot;.all&quot; class=&quot;active&quot;&amp;amp;gt;Show All&amp;amp;lt;/a&amp;amp;gt;&amp;amp;lt;/li&amp;amp;gt;';
				if ( $count &amp;amp;gt; 0 ){
				foreach ( $terms2 as $term ) {
												$termname = strtolower($term-&amp;amp;gt;name);								$termname = str_replace(' ', '-', $termname);
	echo '&amp;amp;lt;li style=&quot;list-style:inline;&quot;&amp;amp;gt;&amp;amp;lt;a href=&quot;javascript:void(0)&quot; title=&quot;&quot; class=&quot;&quot; data-filter=&quot;.'.$termname.'&quot;&amp;amp;gt;'.$term-&amp;amp;gt;name.'&amp;amp;lt;/a&amp;amp;gt;&amp;amp;lt;/li&amp;amp;gt;';
	}
}
// in the above foreach loop, the code will return all the values stored in $terms2 array.

 ?&amp;amp;gt;
		&amp;amp;lt;/ul&amp;amp;gt;
	&amp;amp;lt;/div&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;

Bir sonraki adım, her proje için kategoriyi almak ve onu bir sınıf olarak proje konteynerine yerleştirmektir.

&amp;amp;lt;?php
/*
Get the category for each unique post using the post ID
*/
$terms = get_the_terms( $post-&amp;amp;gt;ID, 'project_categories' );
if ( $terms &amp;amp;amp;&amp;amp;amp; ! is_wp_error( $terms ) ) :
$links = array();
	foreach ( $terms as $term ) {
	$links[] = $term-&amp;amp;gt;name;
	}
	$tax_links = join( &quot; &quot;, str_replace(' ', '-', $links));
	$tax = strtolower($tax_links);
	else :
	$tax = '';
	endif;

	$terms = get_the_terms( $post-&amp;amp;gt;ID, 'project_categories' );
?&amp;amp;gt;

&amp;amp;lt;?php echo '&amp;amp;lt;div class=&quot;project col-sm-6 col-md-4 all project-item '. $tax .'&quot;&amp;amp;gt;';?&amp;amp;gt;

Yukarıdaki kodda, all , project-item ve $tax her proje kapsayıcısına eklenir. $tax , wp-admin içinde ona atadığınız kategori olacaktır. "Tümü" eklemek, bir kullanıcı "tümü" filtresini her tıkladığında portföy sayfasını sıfırlamanıza olanak tanır.

Sonunda, her projenin “all” adında bir sınıfı olmalı ve benim durumumda her projede ayrıca “yeni inşaat” veya “tadilat” olacak. Şimdi, bir kullanıcı kategorilerden birine tıkladığında, sayfa, portföyün ızgara düzenini korurken, yalnızca seçilen kategoriyi görüntülemek için zarif bir şekilde yeniden biçimlendirilir.

eklenti-filtreler-wordpress-portföy-filtrelenmiş

Sonuç olarak, Isotope.js, herhangi bir WordPress sitesinde uygulanabilen çok güçlü bir jQuery eklentisidir. Kurulduktan sonra katalog, galeri veya portföy düzenlerini sıralamak ve filtrelemek için kullanılabilir. Ayrıca, kullanabileceğiniz birden fazla düzen seçeneği vardır. Buradaki tüm seçeneklere göz atın.

Sonunda, projects-page.php bittiğinde şöyle görünüyor:

&amp;amp;lt;?php
/* This is my Projects Portfolio page */

get_header(); 

?&amp;amp;gt;
&amp;amp;lt;div id=&quot;content-full-width&quot; class=&quot;page-wrap&quot;&amp;amp;gt;
    &amp;amp;lt;div class=&quot;container content-wrapper&quot;&amp;amp;gt;
        &amp;amp;lt;div class=&quot;row&quot;&amp;amp;gt;
            &amp;amp;lt;div id=&quot;content-projects&quot; class=&quot;page-wrap2&quot;&amp;amp;gt;
	    &amp;amp;lt;div class=&quot;container content-wrapper&quot;&amp;amp;gt;
	&amp;amp;lt;!-- ============ CONTENT START ============ --&amp;amp;gt;
	        &amp;amp;lt;section id=&quot;project-content&quot;&amp;amp;gt;
		&amp;amp;lt;div id=&quot;intro&quot; class=&quot;row&quot;&amp;amp;gt;
		    &amp;amp;lt;div class=&quot;col-sm-12 text-center&quot;&amp;amp;gt;
			&amp;amp;lt;?php while ( have_posts() ) : the_post(); ?&amp;amp;gt;
			&amp;amp;lt;?php the_content() ?&amp;amp;gt;
			&amp;amp;lt;?php endwhile; // end of the loop. ?&amp;amp;gt;
		    &amp;amp;lt;/div&amp;amp;gt;
		&amp;amp;lt;/div&amp;amp;gt;
	            &amp;amp;lt;div id=&quot;filters-row&quot; class=&quot;row&quot;&amp;amp;gt;
		    &amp;amp;lt;div id=&quot;project-page&quot; class=&quot;col-lg-12&quot;&amp;amp;gt;
		        &amp;amp;lt;ul class=&quot;nav navbar-nav navbar-left&quot; id=&quot;filters&quot;&amp;amp;gt;
			&amp;amp;lt;?php
			$terms2 = get_terms(&quot;project_categories&quot;);
			$count = count($terms2);
			echo '&amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;a href=&quot;javascript:void(0)&quot; title=&quot;&quot; data-filter=&quot;.all&quot; class=&quot;active&quot;&amp;amp;gt;All&amp;amp;lt;/a&amp;amp;gt;&amp;amp;lt;/li&amp;amp;gt;';
			if ( $count &amp;amp;gt; 0 ){
			    foreach ( $terms2 as $term ) {									    $termname = strtolower($term-&amp;amp;gt;name);
			    $termname = str_replace(' ', '-', $termname);
			echo '&amp;amp;lt;li style=&quot;list-style:inline;&quot;&amp;amp;gt;&amp;amp;lt;a href=&quot;javascript:void(0)&quot; title=&quot;&quot; class=&quot;&quot; data-filter=&quot;.'.$termname.'&quot;&amp;amp;gt;'.$term-&amp;amp;gt;name.'&amp;amp;lt;/a&amp;amp;gt;&amp;amp;lt;/li&amp;amp;gt;';
			}
		} ?&amp;amp;gt;
		    &amp;amp;lt;/ul&amp;amp;gt;
		&amp;amp;lt;/div&amp;amp;gt;
	        &amp;amp;lt;/div&amp;amp;gt;
	    &amp;amp;lt;div id=&quot;projects&quot; class=&quot;row&quot;&amp;amp;gt;
	&amp;amp;lt;!-- Start projects Loop --&amp;amp;gt;
		&amp;amp;lt;?php  /* Query the post   */
	$args = array( 'post_type' =&amp;amp;gt; 'projects', 'posts_per_page' =&amp;amp;gt; -1, 'orderby'=&amp;amp;gt;'menu_order','order'=&amp;amp;gt;'ASC' );
	$loop = new WP_Query( $args );
	while ( $loop-&amp;amp;gt;have_posts() ) : $loop-&amp;amp;gt;the_post();
/* Pull category for each unique post using the ID */
	$terms = get_the_terms( $post-&amp;amp;gt;ID, 'project_categories' );
	if ( $terms &amp;amp;amp;&amp;amp;amp; ! is_wp_error( $terms ) ) :
	   $links = array();
	       foreach ( $terms as $term ) {
	       $links[] = $term-&amp;amp;gt;name;
		}
	       $tax_links = join( &quot; &quot;, str_replace(' ', '-', $links));
	       $tax = strtolower($tax_links);
		else :
		$tax = '';
	endif;
	&amp;amp;lt;?php echo '&amp;amp;lt;div class=&quot;project col-sm-6 col-md-4 all project-item '. $tax .'&quot;&amp;amp;gt;';?&amp;amp;gt;
            &amp;amp;lt;a href=&quot;&amp;amp;lt;?php print get_permalink($post-&amp;amp;gt;ID) ?&amp;amp;gt;&quot;&amp;amp;gt;
              &amp;amp;lt;?php echo the_post_thumbnail(); ?&amp;amp;gt;&amp;amp;lt;/a&amp;amp;gt;
              &amp;amp;lt;h4&amp;amp;gt;&amp;amp;lt;?php print get_the_title(); ?&amp;amp;gt;&amp;amp;lt;/h4&amp;amp;gt;
              &amp;amp;lt;?php print get_the_excerpt(); ?&amp;amp;gt;&amp;amp;lt;br /&amp;amp;gt;
              &amp;amp;lt;a class=&quot;btn btn-default&quot; href=&quot;&amp;amp;lt;?php print get_permalink($post-&amp;amp;gt;ID) ?&amp;amp;gt;&quot;&amp;amp;gt;Details&amp;amp;lt;/a&amp;amp;gt;
        &amp;amp;lt;/div&amp;amp;gt; &amp;amp;lt;!-- End individual project col --&amp;amp;gt;
        &amp;amp;lt;?php endwhile; ?&amp;amp;gt;
    &amp;amp;lt;/div&amp;amp;gt;&amp;amp;lt;!-- End Projects Row --&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;&amp;amp;lt;!-- End Container --&amp;amp;gt;
&amp;amp;lt;!-- ============ CONTENT END ============ --&amp;amp;gt;

&amp;amp;lt;?php get_footer(); ?&amp;amp;gt;

Ve bununla, tamamen işlevsel, içerik filtreleme portföy sayfanız olacak!


Seveceğiniz WordPress eklentileri

Geliştiriciler için en çok önerilen eklentilerimizin bir listesi için bu e-kitabı indirin! Tüm bu eklentilerin kullanımının basit olduğunu, sitenizde çok fazla performans gerektirmediğini ve tamamen güvenilir olduğunu gördük.