Category: Wordpress

  • Adding Taxonomy Filter to Admin posts List for Custom Post Type

    Adding Taxonomy Filter to Admin posts List for Custom Post Type

    在 wordpress 默认的文章 class TerminQuery{ public function __construct() { add_filter(‘parse_query’,array($this, ‘query’)); add_action(‘restrict_manage_posts’,array($this, ‘restrict’)); } function query($query) { global $pagenow; global $typenow; $qv = &$query->query_vars; if ($typenow!=’post’ && $pagenow==’edit.php’) { $taxonomies = get_object_taxonomies($typenow); foreach ($taxonomies as $taxonomy) { if (isset($qv[$taxonomy]) && is_numeric($qv[$taxonomy])) { $term = get_term_by(‘id’,$qv[$taxonomy],$taxonomy); $qv[$taxonomy] = ($term ? $term->slug : ”); } } }…

  • 定制 WordPress 的默认 Query

    One of the most powerful features of WordPress is the WP Query. It is what determines what content is displayed on what page. And often you’ll want to modify this query to your specific needs. Some examples: Don’t display posts from Category X on the homepage Increase or decrease the number of posts displayed per…

  • WPML:Get posts also available in other languages

    WPML:Get posts also available in other languages

    文章页面里,在文章的底部显示该篇文章对应的翻译的语种(国旗)和标题。 暂时没有发现有现成的函数或 API 接口。但是从WPML 的表里面不难看出他们的关系,通过一句SQL即可找出当前文章对应文章的ID, 如下: function _post_foot_languages(){ global $post, $wpdb, $sitepress; $active_languages = $sitepress->get_active_languages(); unset($active_languages[ICL_LANGUAGE_CODE]); //print_r(array_keys($active_languages)); $source_language_code = “”; if(ICL_LANGUAGE_CODE != $sitepress->get_default_language()) $source_language_code = ” and source_language_code = ‘”.$sitepress->get_default_language().”‘”; $query =” SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid = ( SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id = {$post->ID}{$source_language_code}) AND language_code in (‘”.join(‘\’,\”,array_keys($active_languages)).”‘)”; //AND language_code !=…

  • wpml translate blogroll Link categories

    我没有找到更好的办法,后来想了另一个办法: 由于WPML 是可以翻译widget title 的,所以我新建一个 blogFoll 的widget,可以填写标题,选择想要显示的分类。 直接粘贴到主题文件的functon.php 里即可。 if (!class_exists(‘LinksList_Widget’)) : class LinksList_Widget extends WP_Widget { const LANG = ‘yaocms’; function __construct() { $widget_ops = array( ‘classname’ => strtolower(get_class($this)) ); $control_ops = array(‘id_base’ => strtolower(get_class($this))); $this->WP_Widget(strtolower(get_class($this)), __(‘Links List Widget’, self::LANG), $widget_ops, $control_ops); } function widget($args, $instance) { extract($args); /* User-selected settings. */ $title =…

  • Get costom post type taxonomies

    Get costom post type taxonomies

    returning array of taxonomies about a custom post type like array( [0] =>taxonomy_name1, [1] =>taxonomy_name2 ) Code function _get_post_taxonomies($post_type) { // Passing an object // Why another var?? $output = ‘objects’; // name / objects $taxonomies = get_object_taxonomies($post_type); /*// Passing a string using get_post_type: return (string) post, page, custom… $post_type = get_post_type($post); $taxonomies = get_object_taxonomies($post_type,…

  • How to register new post type with new taxonomy

    这篇文章我将详细叙说如何在 WordPress 里运用 register_post_type 和 register_taxonomy添加新的文章类型和分类。 例如我们要新建一个文章类型叫 wine, 与之相对应的分类有两个,分别为  type, region。 所以我们大致需要这些数据,我把他们放到一个数组里: $plugin_name = ‘yaocms’; $portfolio_type = array( ‘type’ => ‘wine’, //注册在数据库里的文章类型 ‘name’ => __(‘Wine’, $plugin_name), //后台里显示菜单名称 ‘singular’ => __(‘Wine’, $plugin_name), //单数形式 ‘capability’ => ‘page’, //兼容模式,page 或 post ‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’, ‘page-attributes’), //默认支持的字段,依次是:标题,主编辑区域,特色图片,排序 ‘slug’ => ‘wine-view’, //显示在前端url里: https://yaoyingying.com/{$this slug}/文章名 ‘taxonomy’ => array( //分类学…

  • 让 jQuery Datepicker 兼容 WordPress 和 WPML

    让 jQuery Datepicker 兼容 WordPress 和 WPML

    没什么好说的,直接代码: jQuery(function($){ $.datepicker.regional[‘zh-hans’] = $.datepicker.regional[‘zh_CN’] = { closeText: ‘关闭’, prevText: ‘&#x3c;上月’, nextText: ‘下月&#x3e;’, currentText: ‘今天’, monthNames: [‘一月’,’二月’,’三月’,’四月’,’五月’,’六月’, ‘七月’,’八月’,’九月’,’十月’,’十一月’,’十二月’], monthNamesShort: [‘一’,’二’,’三’,’四’,’五’,’六’, ‘七’,’八’,’九’,’十’,’十一’,’十二’], dayNames: [‘星期日’,’星期一’,’星期二’,’星期三’,’星期四’,’星期五’,’星期六’], dayNamesShort: [‘周日’,’周一’,’周二’,’周三’,’周四’,’周五’,’周六’], dayNamesMin: [‘日’,’一’,’二’,’三’,’四’,’五’,’六’], weekHeader: ‘周’, dateFormat: ‘yy-mm-dd’, firstDay: 1, isRTL: false, showMonthAfterYear: true, yearSuffix: ‘年’}; $.datepicker.setDefaults( $.datepicker.regional[ “<?php echo defined(‘ICL_LANGUAGE_CODE’)?ICL_LANGUAGE_CODE:WPLANG;?>” ] ); }); 最后一行,这儿由于 WPML 和 WORDPRESS 的语言静态变量不一样,值也不同,需要判断然后输出,输出的值可能是 zh-hans,或 zh_CN,所以在第一行声明了两个变量来存储翻译。

  • How to make Popularity Posts Widget compatible with WPML

    How to make Popularity Posts Widget compatible with WPML

    In table $prefix_PopularityPostsWidget  The view record store here like this: There is WPML function wpml_get_language_information can get the post language information by post id, like this wpml_get_language_information($post->ID)   Array ( [locale] => fr_FR [text_direction] => [display_name] => Français [native_name] => Français [different_language] => ) Array ( [locale] => en_US [text_direction] => [display_name] => Anglais [native_name] => English [different_language] => 1 ) If it’s different language, the value will be true (1) Depand on this, now we can modify public function widget in class_popularity-posts-widget.php Inside function widget, find loop foreach ($rows as $row) Replace by the code between: ////////////// Loop /////////////////// foreach ($rows as $row) { } ////////////// End of loop…

  • 为WordPress[gallery]短标签里的图片链接添加更多属性

    为WordPress[gallery]短标签里的图片链接添加更多属性

    Add rel Attribute to Image Links in WordPress Galleries 如果你想给[ gallery ] 里的图片链接加上而外的属性, rel 或者 class 等等,去配合使用 fancybox、 lightbox 等 jQuery 插件。代码如下: If you want to use either Fancybox or Lightbox scripts for image galleries, you need to add the rel attribute to all full size image links. Here is how to do it: add_filter(‘wp_get_attachment_link’, ‘add_gallery_id_rel’); function add_gallery_id_rel($link) { global $post; return…

  • 获取某个分类下所有文章相关的标签

    获取某个分类下所有文章相关的标签

      获取某个分类(Category)下所有文章相关的标签(TAGS) 首先获取分类下的所有文章: query_posts(array( ‘post_type’ => ‘portfolio’, ‘showposts’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘portfolio-class’, ‘terms’ => $the_taxonomy, ‘field’ => ‘term_id’, ) ), ‘orderby’ => ‘date’, ‘order’ => ‘DESC’ ) ); 去过所有posts对象: $portfolio_vars = $wp_query->posts; 这样就可以获取到每个文章的 ID $portfolio_var->ID; 可以通过这个ID 获取到每个文章相关的标签:wp_get_post_terms($portfolio_var->ID, $taxonomy); 然后就可以循环取出各个标题的属性,存储到新的$relate_terms数组里 $portfolio_vars = $wp_query->posts; foreach($portfolio_vars as $portfolio_var){ //$portfolio_ids[] = $portfolio_var->ID; $term_list = wp_get_post_terms($portfolio_var->ID,…

  • Simple WPML language switch

    Simple WPML language switch

    公司客户老喜欢吧 语言选择器 做成这样~ 麻烦~ function icl_post_languages(){ $languages = icl_get_languages(‘skip_missing=0&orderby=code’); if(1 < count($languages)){ $langs[] = ‘<div class=”languages”>’; foreach($languages as $l){ if($l[‘language_code’] == ‘zh-hans’) $l[‘language_code’] = “中文”; $active = $l[‘active’] == 1?”class=’active_language'”:””; $langs[] = ‘<a ‘.$active.’ href=”‘.$l[‘url’].'”>’.$l[‘language_code’].'</a>’; } $langs[] = ‘</div>’; echo join(“\n”, $langs); } } 文章( Post)和 分类目录(category)页面不显示多语言选择链接 if (icl_post_languages()): $post_types=get_post_types(array( ‘public’ => true, ‘_builtin’ =>…

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

  • 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;

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