2013-01-11 1 views
0

WordPress, 당신은 때때로 나를 화나게합니다!사용자 정의 아카이브 기능을 열 Wordpress에 분할 기능

내가 지난 몇 시간 동안 해왔 던 일은 아카이브의 사이드 바를 사용자 정의 날짜 형식 (설정의 글로벌 옵션을 변경하지 않고)으로 표시 할 수있는 방법을 갖기 위해 노력한 것입니다. 작업은 the_date();를 사용하는 것만 큼 간단해야합니다. 2013 년 1 월을 예를 들어 1 월 13 일로 만들고 대신 1 년 동안 한 열을 버리고 다른 한 열을 다른 순서로 만들고 싶습니다.

이 코드를 적용하여 확실히 서식 사용자 지정 기간을 관리했습니다 : http://www.wpbeginner.com/wp-themes/how-to-customize-the-display-of-wordpress-archives-in-your-sidebar/이에 :

아무런 문제가 일한 것
function sidebar_archive_list($arclistno = "24") { 

    global $wpdb; 

     $limit = 0; 

     $year_prev = null; 

     $months = $wpdb->get_results("SELECT DISTINCT MONTH(post_date) AS month , YEAR(post_date) AS year, COUNT(id) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now() and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC"); 

    foreach($months as $month) : 
     $year_current = $month->year; 

     if ($year_current != $year_prev){ 
      if ($year_prev != null){ } 
     } 
    ?> 

    <a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>" title="<?php echo date("F Y", mktime(0, 0, 0, $month->month, 1, $month->year)); ?>"><?php echo date("M y", mktime(0,0,0,$month->month,1,$month->year)); ?></a> 

<?php 

    $year_prev = $year_current; 

    if(++$limit >= $arclistno) { break; } 

    endforeach; 

} 

.

이 함수에서 반환 된 내용을 두 개의 별도 열 div로 분할하려고하면 문제가 발생합니다. 나는 당신이 wp_get_archives ('echo = 0')에서 얻을 수있는 것처럼 함수를 배열로 만들 필요가 있다고 생각한다; (및 일부 추가 코드)하지만 PHP 기술은 훌륭하지 않으며 동일한 종류의 것을 다시 만드는 방법을 알아낼 수 없습니다. 나는 다음과 같이 보일 것입니다 :

array(3) { [0]=> string(86) "January 2013" 
      [1]=> string(90) "September 2012" 
      [2]=> string(82) "March 2012" 
} 

언제 var_dumped. 또한 앵커 링크가 날짜를 둘러싸고있는 이유는 무엇이든 상관없이 염두에 두어야합니다.

나는 내용 분할을 위해 사용하고 코드는 다음과 같습니다

<?php 

    $links = print(sidebar_archive_list()); 
    $archive_n = count($links); 
     for ($i=0;$i<$archive_n;$i++): 
      if ($i<$archive_n/2): 
       $archive_left = $archive_left.'<li>'.$links[$i].'</li>'; 
      elseif ($i>=$archive_n/2): 
       $archive_right = $archive_right.'<li>'.$links[$i].'</li>'; 
    endif; 
     endfor; 

?>     

<div class="group"> 
    <ul class="sb_double_column_list alignleft"> 
     <?php echo $archive_left;?> 
</ul> 

<ul class="sb_double_column_list alignright"> 
     <?php echo $archive_right;?> 
</ul> 
</div> 

어떤 도움이나 조언이 진심으로 감상 할 수있다. 나는 이것으로 진짜 틀에 박힌 상태에있다!

답변

0

조금 뒤죽박죽이 된 후에 나는 wp_get_archives를 자신의 커스텀 함수로 수정하여, wp_get_archives를 독자적으로 사용하려고 할 때와 비슷한 방식으로 코드 생성 열을 전달할 수있게했다.

코드는 다음과 같습니다.

function sidebar_archive_list($args = '') { 

global $wpdb, $wp_locale; 

$defaults = array(
    'limit' => '24', 
    'format' => 'html', 'before' => '', 
    'after' => '', 'show_post_count' => false, 
    'echo' => 0, 'order' => 'DESC', 
); 

$r = wp_parse_args($args, $defaults); 
extract($r, EXTR_SKIP); 


if ('' != $limit) { 
    $limit = absint($limit); 
    $limit = ' LIMIT '.$limit; 
} 

$order = strtoupper($order); 
if ($order !== 'ASC') 
    $order = 'DESC'; 


//filters 
$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r); 

$join = apply_filters('getarchives_join', '', $r); 

$output = ''; 

    $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit"; 

    $key = md5($query); 

    $cache = wp_cache_get('wp_get_archives' , 'general'); 

    if (!isset($cache[ $key ])) { 
     $arcresults = $wpdb->get_results($query); 
     $cache[ $key ] = $arcresults; 
     wp_cache_set('wp_get_archives', $cache, 'general'); 
    } else { 
     $arcresults = $cache[ $key ]; 
    } 


    if ($arcresults) { 
     $afterafter = $after; 

     foreach ((array) $arcresults as $arcresult) { 

      $url = get_month_link($arcresult->year, $arcresult->month); 

      $year = date("y", strtotime($arcresult->year)); 

      $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month_abbrev($wp_locale->get_month($arcresult->month)), $year); 

      if ($show_post_count) 
       $after = '&nbsp;('.$arcresult->posts.')' . $afterafter; 
      $output .= get_archives_link($url, $text, $format, $before, $after); 
     } 
    } 

if ($echo) // If "echo" has been defined as having a value (e.g: 1) then echo $output, if it has no value (0) then return the value. 
    echo $output; 
else 
    return $output; 
} 

는 그냥 functions.php에 드롭하고 sidebar_archive_list()를 사용합니다. 이 함수는 월간 디스플레이 만 지원하며 기본값은 wp_get_archives에서 약간 변경되었습니다. 따라서 배열을 반향하는 것이 아니라 배열을 반환 할 것이고 기본 제한은 24 (한 열 아래 12 개월 및 그 반대로)입니다.

관련 문제