2017-11-01 21 views
0

WordPressの特定のカテゴリのすべての投稿URLをプラグインなしで一覧表示する方法はありますか? 私はPHPに精通していませんが、いくつかの方法で、メソッドがこのカテゴリの投稿URL(ex:カテゴリを "ブログ"と呼ぶことができます)を呼び出すページテンプレートを使用できるかどうかを考えていました。WordPressの特定のカテゴリの投稿URL

答えて

1

レビューWP_QueryとWordPressループ。このような何かが、私が正しく(PHPタグでラップを)お問い合わせを理解していた場合に動作するはず:

$the_query = new WP_Query(array('category_name' => 'blog')); 

if ($the_query->have_posts()) { 
echo '<ul>'; 
while ($the_query->have_posts()) { 
    $the_query->the_post(); 
    echo '<li>' . get_permalink() . '</li>'; 
} 
echo '</ul>'; 
/* Restore original Post Data */ 
wp_reset_postdata(); 
} else { 
// no posts found 
} 
1

あなたはget_postshttps://codex.wordpress.org/Template_Tags/get_posts)を使用することができます。

これは、WP_Queryのスリムなバージョンです。これは、必要な投稿の配列を返します。

$categoryPosts = get_posts(array(
    // Note: The category parameter needs to be the ID of the category, and not the category name. 
    'category' => 1, 
    // Note: The category_name parameter needs to be a string, in this case, the category name. 
    'category_name' => 'Category Name', 
)); 

次に、あなたの投稿をループすることができます(カスタムフィールドを持っている場合は、より多くのフィールド)

foreach($categoryPosts as $categoryPost) { 
    // Your logic 
} 

$categoryPostは、デフォルトで次が含まれますが、これらのフィールドは明らかに取り込まれますが、これは何ですかあなたは配列で利用できるでしょう:

WP_Post Object 
(
    [ID] => 
    [post_author] => 
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] => 
    [comment_status] => 
    [ping_status] => 
    [post_password] => 
    [post_name] => 
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] => 
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] => 
    [post_type] => 
    [post_mime_type] => 
    [comment_count] => 
    [filter] => 
) 
関連する問題