帮助编辑最近的帖子 Wordpress 小部件以同时显示所有 3 种语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1615709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:32:42  来源:igfitidea点击:

Help to edit the Recent Posts Wordpress widget to diplay in all 3 languages at once

wordpresspostwidgetmultilingual

提问by CreativEliza

Site link: http://nuestrafrontera.org/wordpress/

网站链接:http: //nuestrafrontera.org/wordpress/

I want the feed of recent post titles to show in the sidebar for all 3 languages, separated by language. So, for example, under Recent Posts the sidebar would have "English" and then the latest 3 posts in English, then "Espa?ol" and the latest 3 in Spanish and then French. All in a list in the column and appearing on all pages with the sidebar in all languages.

我希望最近帖子标题的提要显示在所有 3 种语言的侧边栏中,按语言分隔。因此,例如,在“最近的帖子”下,侧边栏将包含“英语”,然后是最新的 3 篇英语帖子,然后是“Espa?ol”以及最新的 3 篇西班牙语和法语帖子。所有内容都在列中的列表中,并以所有语言显示在带有侧边栏的所有页面上。

I am using the most current version of Wordpress with the WPML plugin.

我正在使用带有 WPML 插件的最新版本的 Wordpress。

I believe the Wordpress widget for Recent Posts needs to be tweaked to do this. Here is the code (from wp-includes/default-widgets.php):

我相信需要调整最近帖子的 Wordpress 小部件才能做到这一点。这是代码(来自 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
    }
}

回答by sepehr

I'm not familiar with WPML plugin but if you have language specific categories, you can simply do that:

我不熟悉 WPML 插件,但如果您有特定于语言的类别,您可以简单地这样做:

...
<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>
...

By placing this code in your theme sidebar.phpyou'll hopefully done. But what if you want this as a widget? there are two solutions on my mind:

通过将此代码放在您的主题中,sidebar.php您有望完成。但是如果你想把它作为一个小部件呢?我想到了两个解决方案:

First Solution: As you previously mentioned in your question update, you can fork the core! & change the standard WordPress recent posts widget. Here you go by replacing the original widget()method of the WP_Widget_Recent_Postsclass:

第一个解决方案:正如您之前在问题更新中提到的,您可以分叉核心!并更改标准的 WordPress 最近帖子小部件。在这里,您可以替换该类的原始widget()方法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');
    }
...

But I do not prefer to use such a solution, changing the core is not a good idea! Also this could be a bad practice due to portability reasons while you can rewrite WordPress widgets!

但是我不喜欢用这样的解决方案,换内核不是一个好主意!此外,由于可移植性原因,这可能是一种不好的做法,而您可以重写 WordPress 小部件!

The second, yet preferable solution! In your theme's functions.phpplace the code below:

第二个,但更可取的解决方案!在您的主题functions.php位置,代码如下:

<?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! */
?>

Hope it help ;)

希望它有帮助;)