2015-03-26 3 views
-1

Я использую плагин WP, чтобы показывать последние сообщения автора, плагин показывает сообщение в виде списка маркеров, мне нужно, чтобы он отображался в двух столбцах; что можно сделать, отредактировав php-код плагина?последние сообщения автора

function latest_posts_by_author($array) { 
global $post; 

$defaults = array(
    'author' => '', 
    'show' => 5, 
    'excerpt' => 'false', 
    'post_type' => 'post' 
); 

// Sets default author if in loop or on single page.  
if(in_the_loop() || is_single()){ 
    $author_id=$post->post_author; 
    $defaults['author'] = get_the_author_meta('user_login', $author_id); 
} 

// Overrides defaults with shortcode settings and separates into individual varaibles. 
extract(shortcode_atts($defaults, $array)); 

// Checks to make sure an author has been set. 
if(!empty($author)){ 

    // Checks to see if there are multiple authors set. 
    $comma = strpos($author, ','); 
    if($comma === false) { 

     // Gets the author data for a single author. 
     $author_data = get_user_by('login', $author); 
     if(!empty($author_data)) { 
      $args = array(
       'author' => $author_data->ID, 
       'posts_per_page' => $show, 
       'post__not_in' => array($post->ID), 
       'post_type' => $post_type 
      ); 
     } 

    } else { 

     // Gets the author data for multiple authors. 
     $authors = explode(',', $author ); 
     $author_data = ''; 
     foreach($authors as $author_login){ 
      $user = get_user_by('login', $author_login); 
      $author_data .= $user->ID . ','; 
     } 

     $args = array(
      'author' => $author_data, 
      'posts_per_page' => $show, 
      'post__not_in' => array($post->ID) 
     ); 
    } 

    // Gets posts form database 
    $author_query = new WP_Query($args); 

    // Displays posts if available 
    if($author_query) { 
     $html = ''; 
     $html = apply_filters('latestbyauthor_list_before', $html); 
     $html .= '<ul class="latestbyauthor">'; 
     while ($author_query->have_posts()) : $author_query->the_post(); 
      $html .= '<li>'; 
      $html = apply_filters('latestbyauthor_link_before', $html); 

      // Displays a link to the post, using the post title 
      $html .= '<a href="' . get_permalink() . '" title="' . get_the_title() . '">'; 
      $html = apply_filters('latestbyauthor_title_before', $html); 
      $html .= apply_filters('latestbyauthor_title', get_the_title()); 
      $html = apply_filters('latestbyauthor_title_after', $html); 
      $html .= '</a>'; 
      $html = apply_filters('latestbyauthor_link_after', $html); 

      // Displays the post excerpt if "excerpt" has been set to true 
      if($excerpt == 'true'){ 
       $html .= '<p>' . apply_filters('latestbyauthor_excerpt', get_the_excerpt()) . '</p>'; 
      } 

      $html .= '</li>'; 
     endwhile; 
     $html .= '</ul>'; 
     $html = apply_filters('latestbyauthor_list_after', $html); 
    } 

    // Resets Post Data 
    wp_reset_postdata(); 

    // Returns the results 
    return $html; 
} 

} add_shortcode ('latestbyauthor', 'latest_posts_by_author');

ответ

0

Да, вы можете использовать параметр offset.

В принципе сделать 2 отдельных запросов, первый показ, однако многие вам нравится, то следующий использовать «смещение» => х в параметрах, чтобы компенсировать следующий по 5.

например для первых 5:

$defaults = array( 'author' => '', 'show' => 5, 'excerpt' => 'false', 'post_type' => 'post' );

для второго:

$defaults = array( 'author' => '', 'show' => 5, 'excerpt' => 'false', 'post_type' => 'post', 'offset'=> 5 );

https://codex.wordpress.org/Template_Tags/get_posts#Posts_list_with_offset

редактирования: чтобы сделать его работу, вы можете просто скопировать всю функцию и сделать новый с другим именем функции и названием короткого кода. Во втором случае просто компенсируйте, сколько вам нравится, вы можете называть оба коротких кода, и у вас будет 2 списка с 5 сообщениями в каждом.

+0

Спасибо, это работает. Но у меня все еще есть одна проблема, что она показывает одинаковые результаты. Еще раз спасибо за помощь. –

+0

Нет проблем. Какие результаты вы ожидаете и от чего? – cnorthfield

Смежные вопросы