简体   繁体   English

WordPress wp_get_recent_posts总是按标题排序

[英]Wordpress wp_get_recent_posts always order by title

I want to change the defaults of the "wp_get_recent_posts" Function to get and order by title name. 我想更改“ wp_get_recent_posts”函数的默认设置,以按标题名称进行获取和排序。

I know how to do it with 我知道该怎么做

$args=array('orderby'=> "title",'order'=> "ASC");
$recent_posts = $this->wp_get_recent_posts($args);

My question is how can i add these $args to the function wp_get_recent_posts from child-theme functions.php file with a hook/filter without editing other files. 我的问题是如何在不编辑其他文件的情况下,将这些$ args从带有子目录/过滤器的child-theme functions.php文件添加到函数wp_get_recent_posts。

Thanks. 谢谢。

Use function wp_get_recent_posts 使用功能wp_get_recent_posts

<?php 
    $args = array(
      'numberposts' => 10,
      'orderby' => 'post_title',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish'
    );

    $recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?> 

AFAIK there is no direct way to set a new "default" for the function. AFAIK没有直接的方法为该功能设置新的“默认”。

The only feasable way I see is to write your own wrapper-function: 我看到的唯一可行的方法是编写自己的包装函数:

function wp_get_recent_posts_title( $args = array(), $output = ARRAY_A ) {
    $defaults = array( 'orderby' => 'title' );
    $args = wp_parse_args( $args, $defaults );
    return wp_get_recent_posts( $args, $output );
}

You call this function just like the original, it will forward all parameters, with the exception that 'orderby' will be 'title' when it is not set explicitly. 您可以像调用原始函数一样调用此函数,它将转发所有参数,但未明确设置时,“ orderby”将为“ title”。

Of course this will only work where you call it, it will not change any other calls to the original function. 当然,这仅在您调用它的地方起作用,而不会更改对原始函数的任何其他调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM