2014-10-19 6 views
0

나는 정말로 Wordpress와 PHP에서 초보자입니다. 1,2,3,4 : 나는 4 개 개의 카테고리로 분류가 워드 프레스 관리자 페이지의 :
어떻게 워드 프레스 API와 PHP로 이것을 할

enter image description here

나는 내 질문에 분명하다 생각 다음과 같이 내 웹 사이트에 대한 테마를 만들었습니다. 카테고리 1의 마지막 2 개의 게시물이 섹션 (div) 1 (위 그림)에 표시되고, 카테고리 2의 마지막 2 개의 게시물이 섹션 (div) 2에 표시되도록하고 싶습니다.
내가 말했듯이, 나는 초보자이며 Wordpress 문서에서 많은 기능을 접했다.
예를 들어 섹션 2에서 index.php (내 사용자 정의 테마) 아래 코드를 사용했습니다. "죄송합니다. 귀하의 기준과 일치하는 게시물이 없습니다."

<?php 
        $args = array('post' => 'post', 'posts_per_page' => 1,'category_name'=>'تازه ها'); 
        $the_query = new WP_Query($args); 
        ?> 
        <?php 
        if ($the_query->have_posts()) : ?> 
        <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
        <h2><?php the_title(); ?></h2> 
        <div class="entry-content"> 
        <?php the_content(); ?> 
        </div> 
        <?php endwhile; ?> 
        <?php wp_reset_postdata(); ?> 
        <?php else: ?> 
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
        <?php endif; 
       ?> 

코드를 작성하십시오.

+0

태그가 게시물의 카테고리로 변경되었지만 코드가 변경되었습니다. 그래도 모든 것이 정상인 것 같지만 실패했습니다. – JasonStack

답변

2

이 작동합니다 : 네 개의 각 범주의 ID에

$posts = get_posts ("cat=1&showposts=2"); 
if ($posts) 
{ 
    foreach ($posts as $post): 
    setup_postdata($post); ?> 
    <h2><?php the_title(); ?></h2> 
<?php endforeach; 
} 

변경 cat=1을.

+0

어떤 카테고리의 ID를 얻는 방법? – JasonStack

+0

어쩌면 이걸로 할 수 있을까요? '$ category_id = get_cat_ID ('Category Name'); ' – JasonStack

+0

잘 작동 하겠지만, 당신이 wordpress의 백엔드로 가서 포스트 카테고리를 편집한다면 링크는'http://example.com/wp -admin/edit-tags.php? action = edit & taxonomy = category & tag_ID = 1 & post_type = post' 태그 ID = 1은 카테고리 ID입니다. – Howli