【问题标题】:Help to edit the Recent Posts Wordpress widget to diplay in all 3 languages at once帮助编辑最近的帖子 Wordpress 小部件以同时以所有 3 种语言显示
【发布时间】:2010-12-09 14:22:04
【问题描述】:

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

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

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

我认为需要对用于最近帖子的 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
    }
}

【问题讨论】:

  • 是否将不同的语言作为类别进行跟踪?我不熟悉 WPML...
  • 使用 WPML,几乎所有内容都可以由语言定义,对于帖子/页面和类别等内容,您可以指示一个类别是另一种语言的翻译。

标签: wordpress post widget multilingual


【解决方案1】:

我不熟悉 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>
...

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

第一个解决方案:正如您之前在问题更新中提到的,您可以分叉核心!并更改标准的 WordPress 最近帖子小部件。在这里,您可以替换 WP_Widget_Recent_Posts 类的原始 widget() 方法:

...
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');
    }
...

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

第二种,但更可取的解决方案!在您的主题的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! */
?>

希望对您有所帮助;)

【讨论】:

  • 查看posts 表,看看 WPML 是否更改了表并添加了语言字段
  • 我本身不是程序员,但我会不断地修改代码,直到它工作为止。所以,请原谅我的无知,但我在哪里可以看到帖子表?
  • 我刚刚阅读了您对问题的评论,提到在使用 WPML 时可以使用特定于语言的类别,所以最好的方法就是上面的代码!用某种语言的类别 slug 替换类别 slug 并将其放在侧边栏上。顺便说一句,您可以使用 cli、phpMyAdmin、Navicat Lite for MySQL 等查看 MySQL 表结构。bit.ly/3rAIx1
  • 这就是他们所说的问题。侧边栏是在 Wordpress 中动态创建的,所以我不能轻易地将 PHP 代码放入其中。这就是为什么我试图找到一种方法来调整小部件,因为这是动态侧边栏的一部分。
  • 非常感谢您的帮助,但恐怕这超出了我的范围。我试图完全按照您所说的去做,但每次都遇到 PHP 错误。我确定我只是没有正确插入代码,但我只成功地更改了一两行代码,所以我认为这太复杂了。我希望这是一个简单的更改,例如在某处添加语言 =“all”,但现在我知道为什么我更像是一名设计师而不是技术人员。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多