Category: Coding
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
这个方法会导致 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’
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.
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->ID); if (!$already_has_thumb) { $attached_image = get_children( “post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1” ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->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
<?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’);…
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
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;
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 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
这儿以 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
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&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)
新旧界面对比,旧界面中的 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 是啥,自己百度吧! 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 代码,把它放在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…