메뉴 닫기

워드프레스 php wp_get_archives 포스트 아카이브

워드프레스 php ‘wp_get_archives’ 를 활용해 포스트 아카이브를 맘대로 표현할 수 있다.

사용법(Usage)

<?php wp_get_archives( $args ); ?>

기본 사용법(Default Usage)

<?php $args = array(
	'type'            => 'monthly',
	'limit'           => '',
	'format'          => 'html', 
	'before'          => '',
	'after'           => '',
	'show_post_count' => false,
	'echo'            => 1,
	'order'           => 'DESC',
        'post_type'     => 'post'
);
wp_get_archives( $args ); ?>

파라미터(Parameters)

type
(string) The type of archive list to display. Defaults to WordPress settings. Valid values:

  • yearly
  • monthly – Default
  • daily
  • weekly
  • postbypost (posts ordered by post date)
  • alpha (same as postbypost but posts are ordered by post title)
  • limit
    (integer) Number of archives to get. Default is no limit.

    format
    (string) Format for the archive list. Valid values:

  • html – In HTML list (<li>) tags and before and after strings. This is the default.
  • option – In select (<select>) or dropdown option (<option>) tags.
  • link – Within link (<link>) tags.
  • custom – Custom list using the before and after strings.
  • before
    (string) Text to place before the link when using the html or custom for format option. There is no default.

    after
    (string) Text to place after the link when using the html or custom for format option. There is no default.

    show_post_count
    (boolean) Display number of posts in an archive or do not. For use with all type except 'postbypost'.

  • 1 (True)
  • 0 (False) – Default
  • echo
    (boolean) Display the output or return it.

  • 1 (True) – Default
  • 0 (False)
  • order
    (string) How to order the link list (since Version 3.5)

  • ASC – Ascending order (A-Z).
  • DESC – Descending order (Z-A) – Default
  • post_type
    (”string”) Limit archives to a post type. Default is 'post'. (since Version 4.4)

     

    예제(Examples)

    <?php 
    $my_archives=wp_get_archives(array(
    	'type'=>'alpha', 
    	'show_post_count'=>true, 
    	'limit'=>20, 
    	'post_type'=>'post', 
    	'format'=>'html' 
    ));
    	
    print_r($my_archives); 
    
    ?>
    

    지난 12개월(Last Twelve Months)

    <?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 12 ) ); ?>
    

    지난 20개 포스트(Last Twenty Posts)

    <?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 20, 'format' => 'custom' ) ); ?>
    

    드롭다운 박스(Dropdown Box)

    <select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
      <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
      <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
    </select>
    

    간단 활용(Easy use)

    //전체 포스트
    <?php wp_get_archives('type=postbypost'); ?>
    //최근 포스트 10개
    <?php wp_get_archives('type=postbypost&limit=10'); ?>
    

    참조 원문 https://codex.wordpress.org/Function_Reference/wp_get_archives

    Posted in 지식(Knowledge), All

    관련 콘텐츠(Related)

    댓글 (정보 필수X)

    이메일 구독 - Email Subs

    최선을 다해 직접 만든 콘텐츠만 공유합니다.
    We share the best content we have created.

    이메일 구독 - Email Subs

    최선을 다해 직접 만든 콘텐츠만 공유합니다.
    We share the best content we have created.