<ul class="bigarea">
<?php
$terms = get_terms([
'taxonomy' => 'shop_area',
'hide_empty' => false,
'parent'=>0,
]);
foreach( $terms as $term){?>
<li>
<figure>
<?php the_term_thumbnail($term ->term_id, 'post-thumbnail');?>
<figcaption>
<?php echo $term->name; ?>
</figcaption>
</figure>
<ul class="cityname">
<?php
$taxonomy_name = 'shop_area';$termchildren = get_term_children( $term->term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a class="each-item" href="#'.$term->term_id.'">' . $term->name . '</a></li>';?>
<?php }?>
</ul>
</li>
<?php };?>
</ul>
WordPress自定义文章添加分类筛选
/*adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type*/
function todo_restrict_manage_posts() {
global $typenow;
$args=array( 'public' => true, '_builtin' => false,'name' => '' );
$post_types = get_post_types($args);
if ( in_array($typenow, $post_types) ) {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
wp_dropdown_categories(array(
'show_option_all' => __('Show All '.$tax_obj->label ),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'term_order',
'selected' => $_GET[$tax_obj->query_var],
'hierarchical' => $tax_obj->hierarchical,
'show_count' => false,
'hide_empty' => true
));
}
}
}
function todo_convert_restrict($query) {
global $pagenow;
global $typenow;
if ($pagenow=='edit.php') {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$var = &$query->query_vars[$tax_slug];
if ( isset($var) ) {
$term = get_term_by('id',$var,$tax_slug);
$var = $term->slug;
}
}
}
return $query;
}
add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
add_filter('parse_query','todo_convert_restrict');
/*adding-metabox-filter-to-admin-list-for-a-custom-post-type*/
/*
**proavance used
*/
function wisdom_filter_tracked_plugins()
{
global $typenow;
$current_plugin = '';
if (isset($_GET['slug'])) {
$current_plugin = $_GET['slug'];
$_GET['slug'] = "";
}
if ($typenow == 'products') {
$args = array(
'post_type' => 'product_labels',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'meta_key' => '_label_order_key',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brands_category',
'field' => 'slug',
'terms' => 'products_brand',
),
),
);
$new_query = new WP_Query($args);
if ($new_query->have_posts()) {
?>
<select name="slug" id="slug">
<option value="all" <?php selected('all', $current_plugin); ?>>All</option>
<?php
while ($new_query->have_posts()) {
$new_query->the_post();
global $post;
$label_slug = get_post_meta($post->ID, '_label_slug_key', true);
?>
<option value="<?php echo $label_slug; ?>" <?php selected($label_slug, $current_plugin); ?>>
<?php echo $label_slug; ?></option><?php
}
}
?>
</select>
<?php }
}
function wisdom_sort_plugins_by_slug($query)
{
global $pagenow;
// Get the post type
$post_type = isset($_GET['post_type']) ? $_GET['post_type'] : '';
if (is_admin() && $pagenow == 'edit.php' && $post_type == 'products' && isset($_GET['slug']) && $_GET['slug'] != 'all') {
$query->query_vars['meta_key'] = '_brand_value_key';
$query->query_vars['meta_value'] = $_GET['slug'];
$query->query_vars['meta_compare'] = '=';
}
return $query;
}
add_action('restrict_manage_posts', 'wisdom_filter_tracked_plugins');
add_filter('parse_query', 'wisdom_sort_plugins_by_slug');
apply_filters()
apply_filters( string $tag, mixed $value );
创建过滤器。
$tag(字符串)(必须)过滤器的名字。
$value(混合)(必须)要过滤的值,如果没人过滤则直接返回这个值。
例:
<?php
echo apply_filters('the_content', '
');?>
wordpress获取分类别名
function cat_slug(){
$cat = get_query_var('cat');
$yourcat = get_category($cat);
return $yourcat->slug;
}
$category = get_the_category(); echo ‘cat-‘.$category[0]->slug;
category子父母模板
子模板与父级模板相同
function category_template($template){ $category = get_queried_object(); if($category->parent !='0'){ while($category->parent !='0'){ $category = get_category($category->parent); } } $templates = array(); if ( $category ) { $templates[] = "category-{$category->slug}.php"; $templates[] = "category-{$category->term_id}.php"; } $templates[] = 'category.php'; return locate_template( $templates ); } add_filter('category_template', 'category_template');
子模板与父级模板不同
function get_category_parent($parent) { global $cat; $parent=get_category($cat); if($parent->parent)return ture; else return false; }
data-client用法
<body <?php body_class(); ?> data-XXX="<?php if(wp_is_mobile()){ echo 'example';} ?>"></body> body[data-XXX=example] { css: }
获取分类(cat&term)信息
WordPress get_terms()的使用方法:
例1.输出term相关信息
<ul class="products"> <?php $terms = get_terms('category',array( 'with_thumbnail' => true, 'hide_empty' => false, ) ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) {?> <li> <a href="<?php echo get_term_link($term->term_id) ?>"> <div class="term-pic"> <?php the_term_thumbnail($term ->term_id, 'post-thumbnail');?> </div> <div> <h3> <?php echo $term->name;?> </h3> <div> <?php echo $term->description;?> </div> </div> </a> </li> <?php } }?> </ul>
例2.循环term显示相应的文章
<?php $terms = get_terms([ 'taxonomy' => 'area', 'hide_empty' => true, ]); foreach( $terms as $term):?> <div class="area-part"> <h3> <?php echo $term->name; ?> </h3> <ul class="posts-list"> <?php $args = array( 'post_type'=>'store', 'tax_query' => array( array( 'taxonomy' => 'area', 'field' => 'id', 'terms' => $term->term_id, ), ), ); $query = new WP_Query( $args ); if( $query->have_posts()): while($query->have_posts()):$query->the_post(); get_template_part('partials/content','shopList'); //Restore orginial post Data wp_reset_postdata(); endwhile;?> </ul> <?php endif;?> </div> <?php endforeach; ?>
其他
get_terms()默认参数:
$get_terms_default_attributes = array ( 'taxonomy' => 'category', //empty string(''), false, 0 don't work, and return empty array 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, //can be 1, '1' too 'include' => 'all', //empty string(''), false, 0 don't work, and return empty array 'exclude' => 'all', //empty string(''), false, 0 don't work, and return empty array 'exclude_tree' => 'all', //empty string(''), false, 0 don't work, and return empty array 'number' => false, //can be 0, '0', '' too 'offset' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'hierarchical' => true, //can be 1, '1' too 'search' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, //can be 0, '0', '' too 'get' => '', 'child_of' => false, //can be 0, '0', '' too 'childless' => false, 'cache_domain' => 'core', 'update_term_meta_cache' => true, //can be 1, '1' too 'meta_query' => '', 'meta_key' => array(), 'meta_value'=> '', );
get_terms()数组返回值:
array(1) { [0]=> object(WP_Term) (11) { ["term_id"]=> //int ["name"]=> //string ["slug"]=> //string ["term_group"]=> //int ["term_taxonomy_id"]=> //int ["taxonomy"]=> //string ["description"]=> //string ["parent"]=> //int ["count"]=> // int ["filter"]=> //string ["meta"]=> array(0) { // presumably this would be some returned meta-data? } } }
wordpress获取特色图片地址
function get_attachment($size, $id)
{
if (empty($size)) {
$size = ‘medium’;
}
$output = ”;
if (has_post_thumbnail($id)) {
$output = wp_get_attachment_image_url(get_post_thumbnail_id($id), $size);
} else {
$attachments = get_posts(array(
‘post_type’ => ‘attachment’,
‘numberposts’ => 1,
‘post_parent’ => $id,
‘post_mime_type’ => ‘image’,
));
if (!empty($attachments)) {
$output = wp_get_attachment_image_url($attachments[0]->ID, $size);
} else {
$output = get_template_directory_uri() . ‘/assets/img/default.jpg’;
}
}
return $output;
}
wordpress小工具
①注册
/* Register widget area*/ function theme_widgets_init() { register_sidebar( array( 'name' => __( 'Blog Sidebar', 'twentyseventeen' ), 'id' => 'sidebar-1', 'description' => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'twentyseventeen' ), 'before_widget' => '<section id="%1$s" class="widget %2$s">', 'after_widget' => '</section>', 'before_title' => '<h2 class="widget-title">', 'after_title' => '</h2>', ) ); } add_action( 'widgets_init', 'theme_widgets_init' );
②输出
<?php if ( ! is_active_sidebar( 'sidebar-1' ) ) { return; } ?> <aside id="secondary" class="widget-area" role="complementary" aria-label="<?php esc_attr_e( 'Blog Sidebar', '' ); ?>"> <?php dynamic_sidebar( 'sidebar-1' ); ?> </aside><!-- #secondary -->