Author: 迎迎 姚

  • WordPress MetaBox 里创建带编辑器的可重复字段

    WordPress MetaBox 里创建带编辑器的可重复字段

    在创建页面选择某个页面模板后,在 metabox 里会出现下面的模块,带有 tinyMCE 的文本域。 点击 add new,即可以添加一个新的文本域,点击 remove 可以删除对应的文本域。 对于某个页面里有很对相同小模块的比较有用。 效果如下图: /** * Calls the class on the post edit screen */ function xxxx_repeat_competences_fields($cnt, $p = null) { if ($p === null) { $a = ”; $i = ”; $t = ”; } else { $a = $p[‘a’]; $i = $p[‘i’]; $t = wpautop($p[‘t’]);…

  • WordPress 获取首页 ID

    WordPress 获取首页 ID

    <?php echo get_option(‘page_on_front’); //home page ID echo get_option(‘page_for_posts’); ?>  

  • 自动吧上传的第一张图片设置为特色图像

    自动吧上传的第一张图片设置为特色图像

    function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post-&gt;ID); if (!$already_has_thumb) { $attached_image = get_children( “post_parent=$post-&gt;ID&amp;post_type=attachment&amp;post_mime_type=image&amp;numberposts=1” ); if ($attached_image) { foreach ($attached_image as $attachment_id =&gt; $attachment) { set_post_thumbnail($post-&gt;ID, $attachment_id); } } } } add_action(‘the_post’, ‘autoset_featured’); add_action(‘save_post’, ‘autoset_featured’); add_action(‘draft_to_publish’, ‘autoset_featured’); add_action(‘new_to_publish’, ‘autoset_featured’); add_action(‘pending_to_publish’, ‘autoset_featured’); add_action(‘future_to_publish’, ‘autoset_featured’);  

  • WordPress add a theme options page

    WordPress add a theme options page

    <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ add_action(‘admin_menu’, ‘my_theme_options_menu’); function my_theme_options_menu() { add_action(‘admin_print_scripts’, ‘my_options_admin_scripts’); add_action(‘admin_print_styles’, ‘my_options__admin_styles’); add_theme_page(‘My Plugin Theme’, ‘My Theme Options’, ‘administrator’, ‘my-theme-options’, ‘my_theme_options_function’); add_action(‘admin_init’, ‘register_mysettings’); } function register_mysettings() { //register our settings register_setting(‘my_theme_options’, ‘myoption’); } function my_options_admin_scripts() { wp_enqueue_script(‘media-upload’); wp_enqueue_script(‘thickbox’);…

  • 创建带有编辑器的字段,调整模块顺序。

    创建带有编辑器的字段,调整模块顺序。

    国际惯例,效果: First: 创建custom post type ‘slider’ 和 slider 的分类 ‘city’ function yao_codex_custom_init() { $labels = array( ‘name’ => ‘Sliders’, ‘singular_name’ => ‘Slider’, ‘add_new’ => ‘Add New’, ‘add_new_item’ => ‘Add New Slider’, ‘edit_item’ => ‘Edit Slider’, ‘new_item’ => ‘New Slider’, ‘all_items’ => ‘All Slider’, ‘view_item’ => ‘View Slider’, ‘search_items’ => ‘Search Slider’, ‘not_found’ => ‘No Slider…

  • Wordpres add meta box within duplicate image fields

    Wordpres add meta box within duplicate image fields

    这段代码可以实现在 meta box 里加入一个图片组(使用wordpress uploader)上传图片。 <?php function xxxx_repeat_image_fileds($cnt, $p = null) { if ($p === null) { $a = ”; $existing_image = ”; } else { $a = $p[‘n’]; $existing_image = ‘<img src=”‘ . $a . ‘” width=”100″ />’; } return <<<HTML <div><div class=”existing_image”>$existing_image</div><input type=”text” class=”xxxx_image” id=”xxxx_image_$cnt” name=”xxxx_image[$cnt][n]” size=”10″ value=”$a”><span class=”remove”>Remove</span><hr></div> HTML; } function xxxx_render_image_attachment_box($post)…

  • 获取WordPress循环(loop)里的 key

    获取WordPress循环(loop)里的 key

    Get current index position in loop in WordPress 获取当前元素的系列,只需要 $wp_query->current_post while (have_posts()) : the_post(); // Some basic wordpress template tags the_title(); the_content(); // Echo the current index position of the loop (0,1,2,3..etc) echo $wp_query->current_post; endwhile; 或者还有种原始的方法: $key = “”; while (have_posts()) : the_post(); // Some basic wordpress template tags the_title(); the_content(); echo $key; $key++ endwhile;

  • 正则表达式

    正则表达式

    以前写的一段正则抓取安居客经纪人信息的表达式,但是不确定现在还有么有用了! $url=”http://shanghai.anjuke.com/brokerinfo.php?bid={$i}”; $content= file_get_contents($url); //preg_match_all (‘/(.*)[^

  • TimelineJS in WordPress

    TimelineJS in WordPress

    下载: https://github.com/VeriteCo/TimelineJS 演示:http://timeline.verite.co/ 首先要建立一个 custom post type 叫 timeline 添加一些测试数据之后,进行下一步。 取出数据生成JSON $timeline_args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘timeline’ ); $timeline_array = array(); $timeline_list = new WP_Query($timeline_args); $index = 0; while ($timeline_list->have_posts()) : $timeline_list->the_post(); $timeline_array[$index][“startDate”] = get_the_date(“Y,m,d”); $timeline_array[$index][“headline”] = get_the_title(); $timeline_array[$index][“text”] = get_the_excerpt(); $large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), ‘medium’); if (has_post_thumbnail()) { $post_thumbnail = get_post(get_post_thumbnail_id()); $large_image_url…

  • Add delivery date and time for woocommerce

    Add delivery date and time for woocommerce

    /** * Add the field to the checkout * */ add_action(‘woocommerce_after_checkout_billing_form’, ‘my_custom_checkout_field’); function my_custom_checkout_field($checkout) { echo ” . __(‘Prefered delivery time’, ‘woothemes’) . ”; $tomorrow = date(‘Y-m-d’, strtotime(the_date(‘Y-m-d’, ”,”, FALSE) .’ +1 day’)); $after_tomorrow = date(‘Y-m-d’, strtotime(the_date(‘Y-m-d’, ”,”, FALSE) .’ +2 day’)); $_3day_now = date(‘Y-m-d’, strtotime(the_date(‘Y-m-d’,”,”, FALSE) .’ +3 day’)); echo ”; woocommerce_form_field(‘prefered_delivery_date’, array( ‘type’…