gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:13:20 25 4
gpt4 key购买 nike

网站链接: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 插件,但如果您有特定于语言的类别,您可以简单地这样做:

...
<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你会希望完成。但是如果你想把它作为一个小部件呢?我想到了两个解决方案:

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

希望它有帮助;)

关于wordpress - 帮助编辑最近的帖子 Wordpress 小部件以同时显示所有 3 种语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1615709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com