[워드프레스 테마] 포스트 꾸미기 #6

워드프레스 테마 2018. 5. 16. 22:23

[워드프레스 테마 만들기] 포스트 꾸미기



index.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php get_header(); ?>
    <?php
      if(have_posts()) :
          while(have_posts()) : the_post(); ?>
              <article class="post">
                  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                  <p class="post-info"><?php the_time('Y년 n월 j일 a g:i');?>
                   | 글쓴이 <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>">
                     <?php the_author(); ?></a> | 카테고리
                     <?php
                        $categories = get_the_category();
                        $separator = ", ";
                        $output = '';
 
                        // 카테고리 변수의 값이 있으면 $category에 변수에 하나씩 담
                        // 는 다는 이야기다.
                        if($categories) :
                            foreach($categories as $category) :
                                $output .= '<a href="' . get_category_link($category->term_id) .'">'$category->cat_name.$separator'</a>';
                            endforeach;
                            echo trim($output$separator);
                        endif;
                      ?>
                   </p>
                  <?php the_content(); ?>
              </article>
    <?php endwhile;
      else :
          echo '포스트가 존재하지 않습니다.';
      endif;
   ?>
<?php get_footer(); ?>
 
cs



1. 7번째 줄에 있는 <p class="post-info"><?php the_time('Y년 n월 j일 a g:i');?>

함수는 글이 쓰여진 시간을 출력하는 함수다.  시간 출력함수 사용법은 

http://www.everdevel.com/PHP/date.php 이 주소로 찾아서 사용을 하면 된다.



2. 8번째 줄에 있는 <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>">

                     <?php the_author(); ?></a>

함수는 포스트의 글쓴이를 불러오는 함수다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
    $categories = get_the_category();
    $separator = ", ";
    $output = '';
 
    // 카테고리 변수의 값이 있으면 $category에 변수에 하나씩 담
    // 는 다는 이야기다.
    if($categories) :
        foreach($categories as $category) :
            $output .= '<a href="' . get_category_link($category->term_id) .
                '">'$category->cat_name.$separator'</a>';
        endforeach;
        echo trim($output$separator);
    endif;
?>
cs


3. 중간에 있는 이 소스는 포스트 내용의 카데고리를 읽어들어와서 반복문으로 포스트 끝까지 돌아서

$category변수에 넣는코드다. 그리고 반복문을 다 돌고나면 읽어들인 카데고리명을 출력한다.

trim()함수는 뒤에 문자열과 반복된 부분을 제거하는 함수다.



index.php

style.css





설정

트랙백

댓글