WordPress에서 카테고리별 최신 글을 본문에 동적으로 표시하는 방법은 여러 가지가 있습니다. 여기서는 Shortcode를 사용하여 특정 카테고리의 최신 글을 본문에 출력하는 방법을 중점적으로 설명합니다. 이 방법은 유연하며, 사용자가 원하는 페이지나 포스트 내에서 쉽게 적용할 수 있습니다.
Shortcode 사용
Shortcode를 사용하면 간단하게 특정 카테고리의 최신 글을 본문에 동적으로 출력할 수 있습니다. 아래는 이 작업을 수행하는 단계별 가이드입니다.
Step 1: Shortcode 정의
functions.php 파일에 다음 코드를 추가하여 Shortcode를 정의합니다:
function category_latest_posts_shortcode($atts) {
// Shortcode attributes and defaults
$atts = shortcode_atts(array(
'category' => '',
'posts_per_page' => 5,
), $atts, 'category_latest_posts');
// Get the category ID
$category_id = get_cat_ID($atts['category']);
// Query to get latest posts from the specified category
$query = new WP_Query(array(
'cat' => $category_id,
'posts_per_page' => intval($atts['posts_per_page']),
));
// Check if posts are available
if ($query->have_posts()) {
$output = '<ul>';
while ($query->have_posts()) {
$query->the_post();
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$output .= '</ul>';
} else {
$output = 'No posts found in this category.';
}
// Restore original Post Data
wp_reset_postdata();
return $output;
}
add_shortcode('category_latest_posts', 'category_latest_posts_shortcode');
이 코드는 category_latest_posts라는 Shortcode를 정의하며, category와 posts_per_page 속성을 사용하여 카테고리 이름과 표시할 포스트 개수를 동적으로 설정할 수 있습니다.
Step 2: Shortcode 사용
페이지나 포스트에서 다음과 같이 Shortcode를 사용합니다:
[category_latest_posts category="news" posts_per_page="5"]
이 코드는 news 카테고리의 최신 글 5개를 표시합니다. 카테고리 이름과 포스트 개수를 필요에 따라 변경할 수 있습니다.
커스텀 페이지 템플릿 사용
커스텀 페이지 템플릿을 사용하여 특정 카테고리의 최신 글을 표시할 수도 있습니다.
Step 1: 커스텀 페이지 템플릿 생성
테마 폴더에 page-category-posts.php 파일을 생성하고 다음 코드를 추가합니다:
<?php
/* Template Name: Category Posts */
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
// Get the category ID from query string
$category_name = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
$posts_per_page = isset($_GET['posts']) ? intval($_GET['posts']) : 5;
if ($category_name) {
$category_id = get_cat_ID($category_name);
// Query to get latest posts from a specific category
$query = new WP_Query(array(
'cat' => $category_id,
'posts_per_page' => $posts_per_page,
));
// Check if posts are available
if ($query->have_posts()) {
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
} else {
echo 'No posts found in this category.';
}
// Restore original Post Data
wp_reset_postdata();
} else {
echo 'No category specified.';
}
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Step 2: 페이지 생성 및 템플릿 적용
- WordPress 대시보드에서
페이지 > 새로 추가를 클릭합니다. - 페이지 제목을 입력하고, 페이지 속성에서 템플릿으로
Category Posts를 선택합니다. - 페이지를 게시합니다.
Step 3: URL 파라미터를 사용하여 카테고리와 포스트 개수 설정
게시된 페이지에 접근할 때 URL 파라미터를 사용하여 카테고리 이름과 포스트 개수를 설정합니다. 예를 들어:
http://example.com/your-category-posts-page?category=news&posts=5
이 URL은 news 카테고리에서 최신 글 5개를 표시합니다.
요약
- Shortcode 사용:
functions.php파일에 Shortcode를 정의하고[category_latest_posts category="news" posts_per_page="5"]와 같이 사용합니다. - 커스텀 페이지 템플릿 사용: 커스텀 템플릿을 생성하고 URL 파라미터를 통해 카테고리 이름과 포스트 개수를 동적으로 설정합니다.
이 방법들을 통해 WordPress 사이트에서 특정 카테고리의 최신 글을 동적으로 표시할 수 있습니다.
WordPress에서 최신 글을 특정 카테고리에 따라 동적으로 출력하는 방법은 여러 가지가 있습니다. 이 작업은 기본적으로 카테고리 ID를 동적으로 입력받아 해당 카테고리의 최신 글을 표시하는 것을 포함합니다. 이를 위해 Shortcode 또는 커스텀 템플릿을 사용할 수 있습니다. 아래에서는 Shortcode를 사용한 방법을 중점적으로 설명합니다.
1. Shortcode 사용
Shortcode를 사용하면 페이지나 포스트의 내용을 쉽게 확장할 수 있습니다. 이 방법을 통해 사용자가 카테고리 ID를 동적으로 입력하고 해당 카테고리의 최신 글을 표시할 수 있습니다.
Step 1: Shortcode 정의
functions.php 파일에 다음 코드를 추가합니다:
function dynamic_latest_posts_shortcode($atts) {
// Shortcode attributes
$atts = shortcode_atts(array(
'category_id' => 1, // 기본 카테고리 ID
'posts_per_page' => 5, // 표시할 포스트 개수
), $atts, 'latest_posts');
// Query to get latest posts from a specific category
$query = new WP_Query(array(
'cat' => intval($atts['category_id']),
'posts_per_page' => intval($atts['posts_per_page']),
));
// Check if posts are available
if ($query->have_posts()) {
$output = '<ul>';
while ($query->have_posts()) {
$query->the_post();
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$output .= '</ul>';
} else {
$output = 'No posts found in this category.';
}
// Restore original Post Data
wp_reset_postdata();
return $output;
}
add_shortcode('latest_posts', 'dynamic_latest_posts_shortcode');
이 코드는 latest_posts라는 Shortcode를 정의하며, category_id와 posts_per_page라는 두 가지 속성을 사용하여 카테고리 ID와 표시할 포스트 개수를 동적으로 설정할 수 있습니다.
Step 2: Shortcode 사용
페이지나 포스트에서 다음과 같이 Shortcode를 사용합니다:
[latest_posts category_id="3" posts_per_page="10"]
이 코드는 카테고리 ID가 3인 카테고리에서 최신 글 10개를 표시합니다. 카테고리 ID와 포스트 개수를 필요에 따라 변경할 수 있습니다.
2. 위젯 사용
카테고리별 최신 글을 표시하는 위젯을 사용할 수도 있습니다. WordPress 기본 위젯이나 커스텀 위젯을 사용하여 이를 구현할 수 있습니다.
방법:
- 위젯 설치:
- WordPress 대시보드에서
플러그인 > 새로 추가로 이동합니다. - ‘Category Posts Widget’ 플러그인을 검색하고 설치합니다.
- 플러그인을 활성화합니다.
- 위젯 사용:
외모 > 위젯으로 이동합니다.- ‘Category Posts’ 위젯을 사이드바나 원하는 위젯 영역에 추가합니다.
- 위젯 설정에서 카테고리와 표시할 포스트 개수를 선택합니다.
3. 커스텀 페이지 템플릿 사용
커스텀 페이지 템플릿을 사용하여 특정 카테고리의 최신 글을 표시할 수 있습니다.
Step 1: 커스텀 페이지 템플릿 생성
테마 폴더에 page-category-posts.php 파일을 생성하고 다음 코드를 추가합니다:
<?php
/* Template Name: Category Posts */
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
// Get the category ID from query string
$category_id = isset($_GET['cat']) ? intval($_GET['cat']) : 1; // 기본 카테고리 ID
$posts_per_page = isset($_GET['posts']) ? intval($_GET['posts']) : 5; // 기본 포스트 개수
// Query to get latest posts from a specific category
$query = new WP_Query(array(
'cat' => $category_id,
'posts_per_page' => $posts_per_page,
));
// Check if posts are available
if ($query->have_posts()) {
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
} else {
echo 'No posts found in this category.';
}
// Restore original Post Data
wp_reset_postdata();
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Step 2: 페이지 생성 및 템플릿 적용
- WordPress 대시보드에서
페이지 > 새로 추가를 클릭합니다. - 페이지 제목을 입력하고, 페이지 속성에서 템플릿으로
Category Posts를 선택합니다. - 페이지를 게시합니다.
Step 3: URL 파라미터를 사용하여 카테고리와 포스트 개수 설정
게시된 페이지에 접근할 때 URL 파라미터를 사용하여 카테고리 ID와 포스트 개수를 설정합니다. 예를 들어:
http://example.com/your-category-posts-page?cat=3&posts=10
이 URL은 카테고리 ID가 3인 카테고리에서 최신 글 10개를 표시합니다.
요약
- Shortcode 사용:
functions.php파일에 Shortcode를 정의하고[latest_posts category_id="3" posts_per_page="10"]와 같이 사용합니다. - 위젯 사용: ‘Category Posts Widget’ 플러그인을 설치하고 사용하여 카테고리별 최신 글을 표시합니다.
- 커스텀 페이지 템플릿 사용: 커스텀 템플릿을 생성하고 URL 파라미터를 통해 카테고리 ID와 포스트 개수를 동적으로 설정합니다.
이 방법들을 통해 WordPress 사이트에서 특정 카테고리의 최신 글을 동적으로 표시할 수 있습니다.
