[태그:] wordpress

  • WordPress REST API posts 목록에 댓글 카운트 추가하기

    WordPress의 기본 REST API중 posts에는 따로 댓글의 카운트를 따로 뽑아주는 기능이 없어 검색해보니..
    역시나 누군가 만들어둔게 있어 퍼옵니다.

    WordPress /wp-content/themes/{사용 테마} 폴더의 functions.php에 추가후 확인

    // https://stackoverflow.com/a/60048982
    add_action( 'rest_api_init', function() {
    	register_rest_field( 'post', 'comment_count', [
            'get_callback' => function ( $post ) {
                return (int) wp_count_comments( $post['id'] )->approved;
            },
            'schema'       => [
                'description' => 'List number of comments attached to this post.',
                'type'        => 'integer',
            ],
        ] );
    });
  • Custom Fields Part1.

    WordPress에서는 글 작성시 Custom Fields를 이용하여 css를 작성하면 http://ilovetypography.com/같은 블로그처럼 포스팅마다 새로운 디자인의 페이지를 만들어 낼수가 있습니다.

    사용하고 계신 Themes의 header.php파일을 열고 head안쪽으로 아래와 같이 소스를 추가해 줍니다.
    [html]
    <?php //Custom Fields
    if(is_single()) {
    $style = get_post_meta($post->ID, ‘_custom_style’, true);
    if($style) {
    ?>
    <style type="text/css" media="screen, print">
    <?=$style?>
    </style>
    <?php
    }
    }
    ?>
    [/html]

    Themes를 수정하셨다면 글 작성시 아래 이미지와 같이 Custom Fields부분에 내용을 작성 하시면 새로운 디자인의 페이지를 만들어 내실수 있습니다.