2009-10-23 13 views
6
liên kết

Site: http://nuestrafrontera.org/wordpress/Trợ giúp để chỉnh sửa widget Recent Posts Wordpress để diplay ở cả 3 ngôn ngữ cùng một lúc

Tôi muốn các thức ăn của tiêu đề bài viết gần đây để hiển thị trong thanh bên cho tất cả 3 ngôn ngữ, cách nhau bằng ngôn ngữ. Vì vậy, ví dụ, dưới bài viết gần đây, thanh bên sẽ có "tiếng Anh" và sau đó là 3 bài viết mới nhất bằng tiếng Anh, sau đó là "Español" và 3 bài mới nhất bằng tiếng Tây Ban Nha và sau đó là tiếng Pháp. Tất cả trong một danh sách trong cột và xuất hiện trên tất cả các trang với thanh bên trong tất cả các ngôn ngữ.

Tôi đang sử dụng phiên bản Wordpress mới nhất với plugin WPML.

Tôi tin rằng tiện ích Wordpress cho Bài đăng gần đây cần phải được tinh chỉnh để thực hiện việc này. Đây là mã (từ wp-includes/default-widgets.php):

class WP_Widget_Recent_Posts extends WP_Widget { 

    function WP_Widget_Recent_Posts() { 
     $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __("The most recent posts on your blog")); 
     $this->WP_Widget('recent-posts', __('Recent Posts'), $widget_ops); 
     $this->alt_option_name = 'widget_recent_entries'; 

     add_action('save_post', array(&$this, 'flush_widget_cache')); 
     add_action('deleted_post', array(&$this, 'flush_widget_cache')); 
     add_action('switch_theme', array(&$this, 'flush_widget_cache')); 
    } 

    function widget($args, $instance) { 
     $cache = wp_cache_get('widget_recent_posts', 'widget'); 

     if (!is_array($cache)) 
      $cache = array(); 

     if (isset($cache[$args['widget_id']])) { 
      echo $cache[$args['widget_id']]; 
      return; 
     } 

     ob_start(); 
     extract($args); 

     $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title']); 
     if (!$number = (int) $instance['number']) 
      $number = 10; 
     else if ($number < 1) 
      $number = 1; 
     else if ($number > 15) 
      $number = 15; 

     $r = new WP_Query(array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); 
     if ($r->have_posts()) : ?> 
     <?php echo $before_widget; ?> 
     <?php if ($title) echo $before_title . $title . $after_title; ?> 
     <ul> 
     <?php while ($r->have_posts()) : $r->the_post(); ?> 
     <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if (get_the_title()) the_title(); else the_ID(); ?> </a></li> 
     <?php endwhile; ?> 
     </ul> 
     <?php echo $after_widget; ?> 
<?php 
      wp_reset_query(); // Restore global post data stomped by the_post(). 
     endif; 

     $cache[$args['widget_id']] = ob_get_flush(); 
     wp_cache_add('widget_recent_posts', $cache, 'widget'); 
    } 

    function update($new_instance, $old_instance) { 
     $instance = $old_instance; 
     $instance['title'] = strip_tags($new_instance['title']); 
     $instance['number'] = (int) $new_instance['number']; 
     $this->flush_widget_cache(); 

     $alloptions = wp_cache_get('alloptions', 'options'); 
     if (isset($alloptions['widget_recent_entries'])) 
      delete_option('widget_recent_entries'); 

     return $instance; 
    } 

    function flush_widget_cache() { 
     wp_cache_delete('widget_recent_posts', 'widget'); 
    } 

    function form($instance) { 
     $title = esc_attr($instance['title']); 
     if (!$number = (int) $instance['number']) 
      $number = 5; 
?> 
     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
     <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> 

     <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label> 
     <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /><br /> 
     <small><?php _e('(at most 15)'); ?></small></p> 
<?php 
    } 
} 
+0

Liệu các ngôn ngữ khác nhau như theo dõi loại? Tôi không quen với WPML ... –

+0

Với WPML, hầu hết mọi thứ có thể được định nghĩa bằng ngôn ngữ và cho những thứ như bài/trang và danh mục, bạn có thể chỉ ra rằng một thể loại là bản dịch của một ngôn ngữ khác bằng một ngôn ngữ khác. – CreativEliza

Trả lời

0

Tôi không quen thuộc với WPML Plugin nhưng nếu bạn có loại ngôn ngữ cụ thể, bạn chỉ có thể làm điều đó:

... 
<ul class="recent-english-posts"> 
<?php 
    $loop = new WP_Query('cat=' . get_category_by_slug('english')->term_id . '&showposts=3'); 
    if($loop->have_posts()): while($loop->have_posts()): $loop->the_post(); 
?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endwhile; else: ?> 
    No English posts yet! 
<?php endif; ?> 
</ul> 
... 
<ul class="recent-spanish-posts"> 
<?php 
    $loop->query('cat=' . get_category_by_slug('spanish')->term_id . '&showposts=3'); 
    if($loop->have_posts()): while($loop->have_posts()): $loop->the_post(); 
?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endwhile; else: ?> 
    No Spanish posts yet! 
<?php endif; ?> 
</ul> 
... 
<ul class="recent-espanol-posts"> 
<?php 
    $loop->query('cat=' . get_category_by_slug('espanol')->term_id . '&showposts=3'); 
    if($loop->have_posts()): while($loop->have_posts()): $loop->the_post(); 
?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endwhile; else: ?> 
    No Espanol posts yet! 
<?php endif; ?> 
</ul> 
... 

Bằng cách đặt mã này vào chủ đề sidebar.php bạn hy vọng sẽ thực hiện. Nhưng nếu bạn muốn điều này như một widget? có hai giải pháp trong đầu của tôi:

Giải pháp đầu tiên: Như bạn đã đề cập trước đó trong bản cập nhật câu hỏi của mình, bạn có thể chia phần lõi! & thay đổi tiện ích bài đăng gần đây của WordPress chuẩn. Ở đây bạn đi bằng cách thay thế bản gốc widget() phương pháp của lớp WP_Widget_Recent_Posts:

... 
function widget($args, $instance) { 
     $cache = wp_cache_get('widget_recent_posts', 'widget'); 

     /* pre-saving language-specific ids for ease of use & code readability ofcourse! */ 
     $cat_ids = array(
         'en'=>get_category_by_slug('english')->term_id, 
         'sp'=>get_category_by_slug('spanish')->term_id, 
         'es'=>get_category_by_slug('espanol')->term_id 
         ); 

     if (!is_array($cache)) 
       $cache = array(); 

     if (isset($cache[$args['widget_id']])) { 
       echo $cache[$args['widget_id']]; 
       return; 
     } 

     ob_start(); 
     extract($args); 

     $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title']); 
     if (!$number = (int) $instance['number']) 
       $number = 10; 
     else if ($number < 1) 
       $number = 1; 
     else if ($number > 15) 
       $number = 15; 

     /* recent english posts loop */ 
     $r = new WP_Query(array('cat' => $cat_ids['en'], 'showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); 
     if ($r->have_posts()) : ?> 
     <?php echo $before_widget; ?> 
     <?php if ($title) echo $before_title . $title . $after_title; ?> 
     <ul> 
     <?php while ($r->have_posts()) : $r->the_post(); ?> 
     <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if (get_the_title()) the_title(); else the_ID(); ?> </a></li> 
     <?php endwhile; ?> 
     </ul> 

     /* recent spanish posts loop */ 
     $r->query(array('cat' => $cat_ids['sp'], 'showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); 
     if ($r->have_posts()) : ?> 
     <?php echo $before_widget; ?> 
     <?php if ($title) echo $before_title . $title . $after_title; ?> 
     <ul> 
     <?php while ($r->have_posts()) : $r->the_post(); ?> 
     <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if (get_the_title()) the_title(); else the_ID(); ?> </a></li> 
     <?php endwhile; ?> 
     </ul> 

     /* recent espanol posts loop */ 
     $r->query(array('cat' => $cat_ids['es'], 'showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); 
     if ($r->have_posts()) : ?> 
     <?php echo $before_widget; ?> 
     <?php if ($title) echo $before_title . $title . $after_title; ?> 
     <ul> 
     <?php while ($r->have_posts()) : $r->the_post(); ?> 
     <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if (get_the_title()) the_title(); else the_ID(); ?> </a></li> 
     <?php endwhile; ?> 
     </ul> 

     <?php echo $after_widget; ?> 
<?php 
       wp_reset_query(); // Restore global post data stomped by the_post(). 
     endif; 

     $cache[$args['widget_id']] = ob_get_flush(); 
     wp_cache_add('widget_recent_posts', $cache, 'widget'); 
    } 
... 

Nhưng tôi không thích sử dụng một giải pháp như vậy, thay đổi cốt lõi không phải là một ý tưởng tốt! Ngoài ra điều này có thể là một thực tế xấu do lý do di chuyển trong khi bạn có thể viết lại các widget WordPress!

Giải pháp thứ hai, nhưng thích hợp hơn! Trong functions.php vị trí của chủ đề của bạn vào mã bên dưới:

<?php 
    function widget_mytheme_recent_posts(){ 
?> 

    <!-- your new widget code will go there 
      replace this comment by the first block of code in this answer, 
      take care of php code blocks! --> 

<?php    
    } //end of widget_mytheme_recent_posts() 

    if(function_exists('register_sidebar_widget')) 
     register_sidebar_widget(__('Recent Posts'), 'widget_mytheme_recent_posts'); 

    /* the rest of functions.php code will go here, maybe sidebar registering! */ 
?> 

Hy vọng nó sẽ giúp;)

+0

Xem bảng 'posts' và xem liệu WPML có thay đổi bảng thêm một trường ngôn ngữ hay không – sepehr

+0

Tôi không phải là một lập trình viên, nhưng thay vào đó, tôi đang sử dụng mã cho đến khi nó hoạt động. Vì vậy, xin lỗi sự thiếu hiểu biết của tôi nhưng tôi có thể thấy bảng bài viết ở đâu? – CreativEliza

+1

Tôi vừa đọc nhận xét của bạn về câu hỏi đề cập rằng có các danh mục ngôn ngữ cụ thể có sẵn trong khi sử dụng WPML, Vì vậy, cách tốt nhất để làm những gì bạn muốn làm là mã ở trên! thay thế slug category bằng slug category của một số ngôn ngữ và đặt nó trên sidebar. bằng cách này bạn có thể thấy các cấu trúc bảng MySQL bằng cách sử dụng cli, phpMyAdmin, Navicat Lite cho MySQL, vv http://bit.ly/3rAIx1 – sepehr