Category: Coding

  • WordPress Get Root Page ID

    WordPress Get Root Page ID

    this snipet will show you how to get root page id and title in wordpress! global $post; $parent_id = $post->post_parent; $post_id = $post->ID; $ancestors = get_post_ancestors($post_id); $root_id = (!empty($ancestors) ? array_pop($ancestors): $post_id); $child_of = $root_id; $widget_title = get_the_title($root_id);  

  • send_to_editor get audio address and id

    send_to_editor get audio address and id

    这个方法会导致 WordPress 编辑器原本的插入媒体功能失效! /* * To change this template, choose Tools | Templates * and open the template in the editor. */ add_filter(‘media_send_to_editor’, ‘media_editor’, 1, 3); function media_editor($html, $send_id, $attachment ){ //get the media’s guid and append it to the html //$post = get_post($send_id); //$html .= ”.$send_id.'”‘; $html = str_replace(‘<a’, ‘<a=”” smid=”.$send_id, $html); return $html;…

  • Backstretch Errot: has no method ‘backstretch’

    Backstretch Errot: has no method ‘backstretch’

    a simple jQuery plugin that allows you to add a dynamically-resized, slideshow-capable background image to any page or element Uncaught TypeError: Object function (e,t){return new v.fn.init(e,t,n)} has no method ‘backstretch’ maybe many guys got this error Maybe the issue is ’cause you put $.backstretch([“bg.jpg”]);   ahead of jquery.backstretch.js plugin, try switch the postion.

  • jQuery 向上弹出式菜单

    jQuery 向上弹出式菜单

    Slide Up Menu / jQuery 向上弹出式菜单 The jQuery Slide Up Menu it’s a good alternative to other existing menus giving the possibility to position the menu on the bottom of your header image or at the bottom of the window, acting like Windows Start Menu. You can let it behave like a normal menu or…

  • 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’…

  • Add Custom Post Type Columns for wordpress

    Add Custom Post Type Columns for wordpress

    这儿以 costom post portfolio 为例, 为 portfolio 列表页加上 两个分类列。删除时间列。 add_filter( ‘manage_edit-portfolio_columns’, ‘set_custom_edit_portfolio_columns’ ); add_action( ‘manage_portfolio_posts_custom_column’ , ‘custom_portfolio_column’, 10, 2 ); function set_custom_edit_portfolio_columns($columns) { unset($columns[‘date’]); //注销不需要的列 return $columns + array(‘portfolio_category’ => __(‘Categories’),’client_category’=>’Client Terms’); } function custom_portfolio_column( $column, $post_id ) { switch ( $column ) { case ‘portfolio_category’: $terms = get_the_term_list( $post_id , ‘portfolio_category’ , ” ,…

  • icl_link_to_element and icl_object_id

    icl_link_to_element and icl_object_id

    Example usage Example Purpose Produced HTML <?php icl_link_to_element(10); ?> Link to page 10 (support page) <a href=”/support/”>Support</a> <?php icl_link_to_element(10,’post’,__(‘Get help’)); ?> Link to support page with an alternative link text <a href=”/support/”>Get help</a> <?php icl_link_to_element(10,’post’,false, array(‘category’=>’products’,’priority’=>’high’); ?> Link to support page and add two arguments <a href=”/support/?category=products&amp;priority=high”>Support</a> <?php icl_link_to_element(10,’post’,false,’priority’=>’high’,’faq’); ?> <?php icl_link_to_element(3,’tag’); ?> Link to…

  • 修改WordPress里用户页的联系信息(Contact Info)

    修改WordPress里用户页的联系信息(Contact Info)

    新旧界面对比,旧界面中的 aim,jabber,yim 被去掉了,加入了时下大家最熟悉的 facebook,twitter …… add_filter( ‘user_contactmethods’ , ‘update_contact_methods’ , 10 , 1 ); function update_contact_methods( $contactmethods ) { // Add new fields $contactmethods[‘phone’] = ‘Phone’; $contactmethods[‘mobile’] = ‘Mobile’; $contactmethods[‘address’] = ‘Address’; // Remove annoying and unwanted default fields unset($contactmethods[‘aim’]); unset($contactmethods[‘jabber’]); unset($contactmethods[‘yim’]); return $contactmethods; }

  • WPML Coding API and Custom Language Switcher

    WPML Coding API and Custom Language Switcher

    老是打不开这些页面,干脆复制到自己的地盘来了。方便自己也方便大家。 神马?WPML 是啥,自己百度吧! WPML offers functions that can be used in your WordPress theme to provide correct support for multilingual themes. Function Purpose Notes do_action(‘icl_language_selector’) Insert the drop down language selector. Described in the language setup section of the getting started guide. do_action(‘icl_navigation_menu’) Insert the top navigation menu. Described in the navitaionsection of the getting started guide. do_action(‘icl_navigation_breadcrumb’)…

  • WordPress debug code

    WordPress debug code

    WordPress debug 代码,把它放在function 文件里,就会在每页的页脚出现这个,包括当前模板,重写规则,Query 等等。 add_action( ‘template_include’, ‘_childtheme_print_template_name’ ); function _childtheme_print_template_name( $template_name ) { global $childtheme_template_name; $childtheme_template_name = $template_name; add_action( ‘wp_footer’, ‘childtheme_print_template_name’, 100 ); return $template_name; } function childtheme_print_template_name( ) { global $childtheme_template_name, $wp_query, $wp_options; echo ” <div style=’padding-left:50px’> DEBUG INFO: Active Template: “.$childtheme_template_name.”\n”; childtheme_print_matched_rewrite_rule(); echo ‘posts_per_page (default option) = ‘.get_option( ‘posts_per_page’ ).”\n”; echo “\n\nWP_QUERY:…

  • Contact Form 7 的那些事儿

    http://hypercircle.com/customization-of-contact-form-7-plugin.html Additional headers:You can input any header lines into the field and you can insert any tags into any place in each header line, just like other Mail fields. Viz: CC, BCC etc like this: After the form is submitted the viewer is able to see the response messages such as “Your message was sent…