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

帮助编辑最近发布的Wordpress小部件,以同时以所有3种语言显示,wordpress,post,widget,multilingual,Wordpress,Post,Widget,Multilingual,网站链接: 我希望最近的文章标题的提要显示在所有3种语言的侧边栏中,用语言分隔。例如,在最近的帖子中,侧边栏会有“英语”和最新的3篇英语帖子,然后是“西班牙文”和最新的3篇西班牙语和法语帖子。列中的列表中,并以所有语言显示在所有页面的侧边栏中 我正在使用最新版本的Wordpress和WPML插件 我相信最近发布的Wordpress小部件需要调整才能做到这一点。以下是代码(来自wp includes/default widgets.php): class WP\u Widget\u最近的帖子扩展了

网站链接:

我希望最近的文章标题的提要显示在所有3种语言的侧边栏中,用语言分隔。例如,在最近的帖子中,侧边栏会有“英语”和最新的3篇英语帖子,然后是“西班牙文”和最新的3篇西班牙语和法语帖子。列中的列表中,并以所有语言显示在所有页面的侧边栏中

我正在使用最新版本的Wordpress和WPML插件

我相信最近发布的Wordpress小部件需要调整才能做到这一点。以下是代码(来自wp includes/default widgets.php):

class WP\u Widget\u最近的帖子扩展了WP\u Widget{
函数WP_Widget_recents_Posts(){
$widget_ops=array('classname'=>'widget_最近的_条目','description'=>uu(“您博客上的最新帖子”);
$this->WP_小部件('recent-posts','recent-posts',$Widget_-ops);
$this->alt_option_name='widget_recent_entries';
添加_操作('save_post',数组(&$this,'flush_widget_cache');
添加_操作('deleted_post',数组(&$this,'flush_widget_cache');
添加_操作('switch_theme',array(&$this,'flush_widget_cache'));
}
函数小部件($args$instance){
$cache=wp_cache_get('widget_recents_posts','widget');
如果(!是_数组($cache))
$cache=array();
如果(isset($cache[$args['widget\u id']])){
echo$cache[$args['widget_id']];
返回;
}
ob_start();
摘录($args);
$title=apply_filters('widget_title',空($instance['title'])?\uuu('recentposts'):$instance['title']);
if(!$number=(int)$instance['number'])
$number=10;
否则如果($number<1)
$number=1;
否则,如果($number>15)
$number=15;
$r=新的WP_查询(数组('showposts'=>$number,'nopaging'=>0,'post_status'=>publish','caller_get_posts'=>1));
如果($r->have_posts()):?>

我不熟悉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>
...
但是我不喜欢使用这样的解决方案,更改核心不是一个好主意!而且由于可移植性的原因,这可能是一个不好的做法,而您可以重写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! */
?>


希望能有所帮助;)

不同的语言是否作为类别进行跟踪?我不熟悉WPML…使用WPML,几乎所有内容都可以通过语言定义,对于post/页面和类别等内容,您可以指出一个类别是另一个类别在不同语言中的翻译。请参阅
posts
表,并查看WPML是否为我不是一个程序员,我只是在代码上修修补补,直到它工作为止。所以,请原谅我的无知,我在哪里可以看到posts表?我刚刚读到你对这个问题的评论,提到在使用WPML时,有语言特定的类别可用,所以最好的方法是上面的代码!用某种语言的类别slug替换类别slug并将其放在侧栏上。顺便说一下,您可以看到使用cli、phpMyAdmin、Navicat Lite for MySQL等的MySQL表结构。正如他们所说,这里有一个难点。侧栏是在Wordpress中动态创建的,因此我无法轻松地将PHP代码放入其中。这就是为什么我正试图找到一种方法来调整小部件,因为这是动态侧边栏的一部分。我非常感谢您的帮助,但恐怕这超出了我的能力范围。我试图完全按照您所说的做,但每次都会出现PHP错误。我确信我只是没有正确插入代码,但我只成功地更改了一两行代码o我认为这太复杂了。我希望这是一个简单的改变,比如在某处添加一个language=“all”,但现在我知道为什么我更像一个设计师而不是一个技术人员。
...
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');
    }
...
<?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! */
?>