Add and Remove Group Meta Box

<?php
function hhs_get_sample_options() {
	$options = array (
		'Option 1' => 'option1',
		'Option 2' => 'option2',
		'Option 3' => 'option3',
		'Option 4' => 'option4',
	);
	
	return $options;
}
add_action('admin_init', 'hhs_add_meta_boxes', 1);
function hhs_add_meta_boxes() {
	add_meta_box( 'repeatable-fields', 'Repeatable Fields', 'hhs_repeatable_meta_box_display', 'post', 'normal', 'default');
}
function hhs_repeatable_meta_box_display() {
	global $post;
	$repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
	$options = hhs_get_sample_options();
	wp_nonce_field( 'hhs_repeatable_meta_box_nonce', 'hhs_repeatable_meta_box_nonce' );
	?>
	<script type="text/javascript">
	jQuery(document).ready(function( $ ){
		$( '#add-row' ).on('click', function() {
			var row = $( '.empty-row.screen-reader-text' ).clone(true);
			row.removeClass( 'empty-row screen-reader-text' );
			row.insertBefore( '#repeatable-fieldset-one tbody>tr:last' );
			return false;
		});
  	
		$( '.remove-row' ).on('click', function() {
			$(this).parents('tr').remove();
			return false;
		});
	});
	</script>
  
	<table id="repeatable-fieldset-one" width="100%">
	<thead>
		<tr>
			<th width="40%">Name</th>
			<th width="12%">Select</th>
			<th width="40%">URL</th>
			<th width="8%"></th>
		</tr>
	</thead>
	<tbody>
	<?php
	
	if ( $repeatable_fields ) :
	
	foreach ( $repeatable_fields as $field ) {
	?>
	<tr>
		<td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
	
		<td>
			<select name="select[]">
			<?php foreach ( $options as $label => $value ) : ?>
			<option value="<?php echo $value; ?>"<?php selected( $field['select'], $value ); ?>><?php echo $label; ?></option>
			<?php endforeach; ?>
			</select>
		</td>
	
		<td><input type="text" class="widefat" name="url[]" value="<?php if ($field['url'] != '') echo esc_attr( $field['url'] ); else echo 'http://'; ?>" /></td>
	
		<td><a class="button remove-row" href="#">Remove</a></td>
	</tr>
	<?php
	}
	else :
	// show a blank one
	?>
	<tr>
		<td><input type="text" class="widefat" name="name[]" /></td>
	
		<td>
			<select name="select[]">
			<?php foreach ( $options as $label => $value ) : ?>
			<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
			<?php endforeach; ?>
			</select>
		</td>
	
		<td><input type="text" class="widefat" name="url[]" value="http://" /></td>
	
		<td><a class="button remove-row" href="#">Remove</a></td>
	</tr>
	<?php endif; ?>
	
	<!-- empty hidden one for jQuery -->
	<tr class="empty-row screen-reader-text">
		<td><input type="text" class="widefat" name="name[]" /></td>
	
		<td>
			<select name="select[]">
			<?php foreach ( $options as $label => $value ) : ?>
			<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
			<?php endforeach; ?>
			</select>
		</td>
		
		<td><input type="text" class="widefat" name="url[]" value="http://" /></td>
		  
		<td><a class="button remove-row" href="#">Remove</a></td>
	</tr>
	</tbody>
	</table>
	
	<p><a id="add-row" class="button" href="#">Add another</a></p>
	<?php
}
add_action('save_post', 'hhs_repeatable_meta_box_save');
function hhs_repeatable_meta_box_save($post_id) {
	if ( ! isset( $_POST['hhs_repeatable_meta_box_nonce'] ) ||
	! wp_verify_nonce( $_POST['hhs_repeatable_meta_box_nonce'], 'hhs_repeatable_meta_box_nonce' ) )
		return;
	
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
		return;
	
	if (!current_user_can('edit_post', $post_id))
		return;
	
	$old = get_post_meta($post_id, 'repeatable_fields', true);
	$new = array();
	$options = hhs_get_sample_options();
	
	$names = $_POST['name'];
	$selects = $_POST['select'];
	$urls = $_POST['url'];
	
	$count = count( $names );
	
	for ( $i = 0; $i < $count; $i++ ) {
		if ( $names[$i] != '' ) :
			$new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
			
			if ( in_array( $selects[$i], $options ) )
				$new[$i]['select'] = $selects[$i];
			else
				$new[$i]['select'] = '';
		
			if ( $urls[$i] == 'http://' )
				$new[$i]['url'] = '';
			else
				$new[$i]['url'] = stripslashes( $urls[$i] ); // and however you want to sanitize
		endif;
	}
	if ( !empty( $new ) && $new != $old )
		update_post_meta( $post_id, 'repeatable_fields', $new );
	elseif ( empty($new) && $old )
		delete_post_meta( $post_id, 'repeatable_fields', $old );
}
?>

获取当前文章父级分类别名

Use the ID value returned by $category[0]->category_parent and pass it through get_term(). Example:

$category = get_the_category(); 
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id != 0 ) {
    $category_parent = get_term( $category_parent_id, 'category' );
    $css_slug = $category_parent->slug;
} else {
    $css_slug = $category[0]->slug;
}

自动获取序号

html:

<ul class="num">
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
        <li><span></span></li>
</ul>

js:

function addNum() {
var $items = $('.num').find('li').length;
for( var $i = 0; $i < $items; $i++) {
var a = $i + 1;
if(a<10){
$('.num').find('li').eq($i).find('span').html(a).prepend('0');}
else{
$('.num').find('li').eq($i).find('span').html(a);
}
}
}
$(function() {addNum();})

获取最大高度

function ResizeAll() {
        var $box = $('.works-box .item');
        $box.css({
            "height": "auto"
        });
        $box.height(Math.max.apply(null, $box.map(function () {
            return $(this).height();
        })));
    }

wordpress不同模板获取不同的meta信息

//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);

相关文章查询方法

<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');