Помогите отредактировать Недавние Сообщения виджет Wordpress к diplay на всех 3 языках сразу

Взгляните на SQL-подсказку Red Gate .

6
задан CreativEliza 25 October 2009 в 11:25
поделиться

1 ответ

I ' я не знаком с плагином 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. Здесь вы можете заменить исходный метод 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');
    }
...

Но я не предпочитаю использовать такое решение, менять ядро ​​- не лучшая идея ! Также это может быть плохой практикой из-за соображений переносимости, поскольку вы можете переписывать виджеты WordPress!

Второе, но все же предпочтительное решение! В вашей теме functions.

0
ответ дан 18 December 2019 в 04:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: