//META DESCRIPTION FUNCTION
function seometadescription(){
global $post, $posts;
if ( is_single() || is_page() ) :
$customDesc = 'meta_desc';
if ( have_posts() ) : while ( have_posts() ) : the_post();
$descriere = get_post_meta($post->ID, $customDesc, true);
endwhile;
endif;
elseif( is_category() ):
$descriere = category_description();
elseif( is_front_page() ) :
$descriere = 'some words for front page description';
endif;
$customMetaDesc = '<meta name="description" content="'.$descriere.'" />';
echo $customMetaDesc;
}
// end meta desc function
iframe插入视频,点击按钮播放
<iframe src="https://www.youtube.com/embed/gc9oMqXZXGM?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<script>
$('.button').on('click',function(){
$('.parentbox').find('iframe')[0].contentWindow.postMessage('{"event":"command","func":"' + 'playVideo' + '","args":""}', '*');
})
</script>
添加经典样式编辑器
add_meta_box('metabox5','Gallary','box_callback',array(''),'normal');
function box_callback($post){
wp_nonce_field('box5_nonceaction','box5_noncename');
$value = get_post_meta( $post->ID, '_box5_value_key', true );
wp_editor( htmlspecialchars_decode($value), $editor_id, $settings = array(
'quicktags'=> 0,
'tinymce'=>0,
'textarea_rows'=>4,
'editor_class'=>"textareastyle"
)); }
$post_ID = $_POST['post_ID'];
$mydata = $_POST[ $editor_id];
update_post_meta($post_ID,'_box5_value_key',$mydata);
判断滚动位置
function animateContent() {
var $object = $("a,b,c");
$object.each(function () {
var $this = $(this);
if ($this.length) {
var $a = $(this).offset().top;
var $b = $(window).scrollTop();
var $c = $(window).height() * 0.75;
var $d = $(window).height();
var $selfHeight = $(this).height();
if (($b > ($a - $c)) || ($b > ($a - $d + $selfHeight))) {
$this.addClass("animate");
}
}
});
}
相关文章查询方法
<div class="related-content">
<?php global $post; $tags = wp_get_post_terms( $post->ID, '$taxonomy' );
if ( $tags ) {
$first_tag = $tags[ 0 ]->term_id;
$args = array(
'post_type' => '$post-type',
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => '$taxonomy',
'field' => 'term_id',
'terms' => $first_tag,
),
),
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ): $my_query->the_post();?>
/* ================
Loop
===============*/
<?php endwhile;wp_reset_postdata();
}}
else {
echo '<p>* 暂无相关文章</p>';
}?>
</div>
查询父级分类的子分类
<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; }