iBegins
 

Thêm bộ lọc filter danh mục category wp-admin WordPress

Thêm bộ lọc filter danh mục category wp-admin WordPress, bạn gắn code functions vào functions.php của theme wordpress. Khi cho code này vào nó sẽ hiện toàn bộ cho tất cả post type.

Dưới đây là code:

<span class="keyword">function</span> <span class="user_function">filter_cars_by_taxonomies</span>( <span class="variable">$post_type</span><span class="operator">,</span> <span class="variable">$which</span> ) {

	<span class="comment">// Apply this only on a specific post type</span>
	<span class="keyword">if</span> ( <span class="string">'car'</span> <span class="operator">!==</span> <span class="variable">$post_type</span> )
		<span class="keyword">return</span><span class="operator">;</span>

	<span class="comment">// A list of taxonomy slugs to filter by</span>
	<span class="variable">$taxonomies</span> <span class="operator">=</span> <span class="function">array</span>( <span class="string">'manufacturer'</span><span class="operator">,</span> <span class="string">'model'</span><span class="operator">,</span> <span class="string">'transmission'</span><span class="operator">,</span> <span class="string">'doors'</span><span class="operator">,</span> <span class="string">'color'</span> )<span class="operator">;</span>

	<span class="keyword">foreach</span> ( <span class="variable">$taxonomies</span> <span class="keyword">as</span> <span class="variable">$taxonomy_slug</span> ) {

		<span class="comment">// Retrieve taxonomy data</span>
		<span class="variable">$taxonomy_obj</span> <span class="operator">=</span> get_taxonomy( <span class="variable">$taxonomy_slug</span> )<span class="operator">;</span>
		<span class="variable">$taxonomy_name</span> <span class="operator">=</span> <span class="variable">$taxonomy_obj</span><span class="operator">-></span><span class="oo">labels</span><span class="operator">-></span><span class="oo">name</span><span class="operator">;</span>

		<span class="comment">// Retrieve taxonomy terms</span>
		<span class="variable">$terms</span> <span class="operator">=</span> get_terms( <span class="variable">$taxonomy_slug</span> )<span class="operator">;</span>

		<span class="comment">// Display filter HTML</span>
		<span class="function">echo</span> <span class="string">"<select name='<span class="variable">{$taxonomy_slug}</span>' id='<span class="variable">{$taxonomy_slug}</span>' class='postform'>"</span><span class="operator">;</span>
		<span class="function">echo</span> <span class="string">'<option value="">'</span> <span class="operator">.</span> <span class="function">sprintf</span>( esc_html__( <span class="string">'Show All %s'</span><span class="operator">,</span> <span class="string">'text_domain'</span> )<span class="operator">,</span> <span class="variable">$taxonomy_name</span> ) <span class="operator">.</span> <span class="string">'</option>'</span><span class="operator">;</span>
		<span class="keyword">foreach</span> ( <span class="variable">$terms</span> <span class="keyword">as</span> <span class="variable">$term</span> ) {
			<span class="function">printf</span>(
				<span class="string">'<option value="%1$s" %2$s>%3$s (%4$s)</option>'</span><span class="operator">,</span>
				<span class="variable">$term</span><span class="operator">-></span><span class="oo">slug</span><span class="operator">,</span>
				( ( <span class="function">isset</span>( <span class="variable">$_GET</span>[<span class="variable">$taxonomy_slug</span>] ) <span class="operator">&&</span> ( <span class="variable">$_GET</span>[<span class="variable">$taxonomy_slug</span>] <span class="operator">==</span> <span class="variable">$term</span><span class="operator">-></span><span class="oo">slug</span> ) ) <span class="operator">?</span> <span class="string">' selected="selected"'</span> <span class="operator">:</span> <span class="string">''</span> )<span class="operator">,</span>
				<span class="variable">$term</span><span class="operator">-></span><span class="oo">name</span><span class="operator">,</span>
				<span class="variable">$term</span><span class="operator">-></span><span class="oo">count</span>
			)<span class="operator">;</span>
		}
		<span class="function">echo</span> <span class="string">'</select>'</span><span class="operator">;</span>
	}

}
add_action( <span class="string">'restrict_manage_posts'</span><span class="operator">,</span> <span class="string">'filter_cars_by_taxonomies'</span> <span class="operator">,</span> <span class="numeric">10</span><span class="operator">,</span> <span class="numeric">2</span>)<span class="operator">;</span>

// Apply this only on a specific post type bạn sửa phần này để tránh các loại post type khác.
// A list of taxonomy slugs to filter by bạn sửa phần này theo danh mục mà post type đang có

Bộ lọc filter danh mục category wp-admin

Chúc các bạn thành công.

Ngày 22 Tháng Bảy, 2019 ( 515 )

Tagged with: , , , , ,

Bình luận bài viết

Đăng nhập để bình luận và xem các bình luận khác.