从自定义分类中查询自定义帖子

时间:2013-03-07 作者:Sebastian

我想做一个custo幻灯片,但我在查询帖子时遇到了问题。我定义了一个自定义帖子类型“slider”和一个自定义分类法“slideshow”:

自定义帖子类型:

function wms_slider_init() {
$labels = array(
\'name\' => \'Slider\',
\'singular_name\' => \'Slider\',
\'add_new\' => \'Add Slider\',
\'add_new_item\' => \'Add New Slider\',
\'edit_item\' => \'Edit Slider\',
\'new_item\' => \'New Slider\',
\'all_items\' => \'All Sliders\',
\'view_item\' => \'View Slider\',
\'search_items\' => \'Search Sliders\',
\'not_found\' =>  \'No Slider found\',
\'not_found_in_trash\' => \'No Slider found in Trash\', 
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Sliders\'
  );

  $args = array(
\'labels\' => $labels,
\'description\'   => \'Holds our Slider poste specific data\',
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true, 
\'show_in_menu\' => true, 
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'slider\' ),
\'capability_type\' => \'post\',
\'has_archive\' => false, 
\'hierarchical\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'page-attributes\')
  ); 

  register_post_type( \'slider\', $args );
}
add_action( \'init\', \'wms_slider_init\' );
自定义分类法:

add_action( \'init\', \'wms_create_slider_taxonomies\', 0 );

//create SlideShow Category for the post type "slider"
function wms_create_slider_taxonomies() 
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
\'name\'                => _x( \'SlideShows\', \'taxonomy general name\' ),
\'singular_name\'       => _x( \'SlideShow\', \'taxonomy singular name\' ),
\'search_items\'        => __( \'Search Genres\' ),
\'all_items\'           => __( \'All SlideShows\' ),
\'parent_item\'         => __( \'Parent SlideShow\' ),
\'parent_item_colon\'   => __( \'Parent SlideShow:\' ),
\'edit_item\'           => __( \'Edit SlideShow\' ), 
\'update_item\'         => __( \'Update SlideShow\' ),
\'add_new_item\'        => __( \'Add New SlideShow\' ),
\'new_item_name\'       => __( \'New SlideShow Name\' ),
\'menu_name\'           => __( \'SlideShow\' )
  );    

  $args = array(
\'hierarchical\'        => true,
\'labels\'              => $labels,
\'show_ui\'             => true,
\'show_admin_column\'   => true,
\'query_var\'           => true,
\'rewrite\'             => array( \'slug\' => \'slideshow\' )
 );

  register_taxonomy( \'slideshow\', array( \'slider\' ), $args );
}
我在分类“slideshow”下创建了一个名为“homeslide”的“类别”,并在其中添加了一个名为“test”的“Slide”。我试图设置一个查询来检索“homeslide”中的所有帖子,但查询不起作用。

这是我的疑问和回答

function wms_output_home_slides($cat) { // $cat = \'homeslide\'
  $home_slides_args = array(
            \'post_type\' => \'slider\',
            \'posts_per_page\'  => -1,
            \'tax_query\' => array(
            array(
                \'taxonomy\' => \'slideshow\',
                \'field\' => \'slug\',
                \'terms\' => $cat
                   )
                 )
);
$sliders_query = new WP_Query( $home_slides_args );
//$print_out = \'<pre>\'.print_r($home_slides_args, true).\'</pre>\';
//loop
if ($sliders_query->have_posts()) : 
    $print_out .= \'<div id="pageBackground">\';
    while  ($sliders_query->have_posts()) : $sliders_query->the_post();
        $slider_id          = $post->ID;

        $print_out .= \'<div class="one-slide">\';

            $print_out .= $slider_id;

        $print_out .= \'</div><!-- one-slide -->\';
    endwhile;
    $print_out .= \'</div><!-- #pageBackground -->\';
endif;


return $print_out;
}
有什么想法吗?为什么没有得到帖子ID?

1 个回复
最合适的回答,由SO网友:Vinod Dalvi 整理而成

您应该使用global关键字访问php中的全局变量。它使变量(或对象、数组)在我们正在处理的当前函数中可见。在wms\\u output\\u home\\u slides函数中,您直接使用$post全局变量,而不是使用全局关键字访问它,如下所示。

function wms_output_home_slides($cat) { // $cat = \'homeslide\'
global $post;
// You Code Goes here
}
查找有关的更多信息this page.

结束