Get Posts from custom taxanomy

别说谁变了你拦得住时间么 提交于 2019-12-11 20:55:40

问题


I want to get posts with custom taxonomy, you can check the code below ppctrainings is the custom taxonomy category and 94 is the id of category.

<?php 
$count = 0;
// this is the custom taxonamy ppctrainings the id is 94
$posts = get_posts('ppctrainings=94&numberposts=10'); 
foreach($posts as $post) { 
if($count == 3){
echo '</tr><tr >';
$count = 0; 
}
$count++;
?>

回答1:


<?php
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'sliders_category', // taxonomy category
            'field' => 'id',
            'terms' => '2' // category id
        )
    ),
    'post_type'=>'sliders',  //name of post type 
    'order'=>'ASC',
    'posts_per_page'=>10
);
    query_posts($args);
    while ( have_posts() ) : the_post(); 
?>

 // your code for display  title and content

<?php   
    endwhile; 
    wp_reset_query();
?>



回答2:


Use the below code for getting the custom post type data.

$args = array(
'posts_per_page' => '-1',
'post_type' => 'blog',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'post_date');

$the_query = new WP_Query($args);

while ($the_query->have_posts()) {
                        $the_query->the_post();
    // Write your code here
}



回答3:


There are many ways you can query the WordPress database

  • pre_get_posts action
  • query_posts()
  • the WP_Query


来源:https://stackoverflow.com/questions/24358582/get-posts-from-custom-taxanomy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!