在过去的一天左右,我一直在用头撞我的桌子。。。我已经创建了一个名为“广告”的自定义帖子类型,并在该帖子类型中创建了一个名为“组”的自定义分类法。我已经创建了一堆不同的帖子,并将它们分配给我创建的各个组,但问题是,当我试图查询这些帖子以获取我的自定义分类法时,结果是空的。
当我尝试用默认类别查询一篇常规文章时,它工作得很好,所以我开始认为这与我设置分类法的方式有关。我已经尝试重置permalinks,但没有成功。
以下是我目前掌握的情况:
如果有人能告诉我哪里出了问题,那就太好了。
谢谢
/*-----------------------------------------------------------------------------------*/
/* Initialize the post type
/*-----------------------------------------------------------------------------------*/
/**
* Custom Post Type: Advertisements
*
* @package WordPress
* @subpackage ExportWise
*/
/* Register custom taxonomies on the \'init\' hook. */
add_action( \'init\', \'register_taxonomy_groups\' );
function register_taxonomy_groups() {
$labels = array(
\'name\' => __( \'Groups\', \'bd\' ),
\'singular_name\' => __( \'Group\', \'bd\' ),
\'search_items\' => __( \'Search Groups\', \'bd\' ),
\'popular_items\' => __( \'Popular Groups\', \'bd\' ),
\'all_items\' => __( \'All Groups\', \'bd\' ),
\'parent_item\' => __( \'Parent Group\', \'bd\' ),
\'parent_item_colon\' => __( \'Parent Group:\', \'bd\' ),
\'edit_item\' => __( \'Edit Group\', \'bd\' ),
\'update_item\' => __( \'Update Group\', \'bd\' ),
\'add_new_item\' => __( \'Add New Group\', \'bd\' ),
\'new_item_name\' => __( \'New Group\', \'bd\' ),
\'separate_items_with_commas\' => __( \'Separate groups with commas\', \'bd\' ),
\'add_or_remove_items\' => __( \'Add or remove groups\', \'bd\' ),
\'choose_from_most_used\' => __( \'Choose from the most used groups\', \'bd\' ),
\'menu_name\' => __( \'Groups\', \'bd\' ),
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'show_in_nav_menus\' => false,
\'show_ui\' => true,
\'show_tagcloud\' => false,
\'show_admin_column\' => true,
\'hierarchical\' => true,
\'rewrite\' => true,
\'query_var\' => true
);
register_taxonomy( \'group\', array(\'advertisement\'), $args );
}
/* Register custom post types on the \'init\' hook. */
add_action( \'init\', \'my_register_post_types\', 20 );
/**
* Registers post types.
*
* @since 0.1.0
* @access public
* @return void
*/
function my_register_post_types() {
/* Set up the arguments for the post type. */
$args = array(
\'public\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => true,
\'show_in_nav_menus\' => false,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 20,
\'menu_icon\' => \'dashicons-chart-line\',
\'can_export\' => true,
\'delete_with_user\' => false,
\'hierarchical\' => true,
\'has_archive\' => false,
\'query_var\' => true,
\'rewrite\' => array(
\'with_front\' => false,
\'pages\' => true,
\'feeds\' => true,
),
\'supports\' => array( \'title\' ),
\'taxonomies\' => array( \'group\' ),
\'labels\' => array(
\'name\' => __( \'Advertisements\', \'bd\' ),
\'singular_name\' => __( \'Advertisement\', \'bd\' ),
\'menu_name\' => __( \'Advertisements\', \'bd\' ),
\'name_admin_bar\' => __( \'Advertisements\', \'bd\' ),
\'add_new\' => __( \'Add New\', \'bd\' ),
\'add_new_item\' => __( \'Add New Advertisement\', \'bd\' ),
\'edit_item\' => __( \'Edit Advertisement\', \'bd\' ),
\'new_item\' => __( \'New Advertisement\', \'bd\' ),
\'view_item\' => __( \'View Advertisement\', \'bd\' ),
\'search_items\' => __( \'Search Advertisements\', \'bd\' ),
\'not_found\' => __( \'No advertisements found\', \'bd\' ),
\'not_found_in_trash\' => __( \'No advertisements found in trash\', \'bd\' ),
\'all_items\' => __( \'All Advertisements\', \'bd\' ),
\'parent_item\' => __( \'Parent Advertisement\', \'bd\' ),
\'parent_item_colon\' => __( \'Parent Advertisement:\', \'bd\' ),
)
);
/* Register the post type. */
register_post_type(
\'advertisement\',
$args
);
}
/*-----------------------------------------------------------------------------------*/
/* Add custom taxonomies to the post type
/*-----------------------------------------------------------------------------------*/
function create_my_taxonomies() {
register_taxonomy(
\'group\',
\'advertisement\',
array(
\'labels\' => array(
\'name\' => \'Groups\',
\'add_new_item\' => \'Add New Group\',
\'new_item_name\' => "New Group"
),
\'show_ui\' => true,
\'show_tagcloud\' => false,
\'hierarchical\' => true,
\'public\' => true
)
);
}
add_action( \'init\', \'create_my_taxonomies\', 0 );
/*-----------------------------------------------------------------------------------*/
/* Query the CPT for Posts from a specific custom taxonomy
/*-----------------------------------------------------------------------------------*/
// If on an Article, display random advertisements
if ( is_single() ) {
$posts_per_page = 1;
$orderby = \'rand\';
// Otherwise display the latest advertisement
} else {
$posts_per_page = 1;
$orderby = \'date\';
}
$args = array(
\'post_type\' => \'advertisement\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'group\',
\'terms\' => array( \'homepage\' ),
\'field\' => \'slug\',
),
),
\'posts_per_page\' => $posts_per_page,
\'orderby\' => $orderby,
);
$query_advertisements = new WP_Query( $args );
//echo $query_advertisements->request; ?>
<?php if ( $query_advertisements->have_posts() ) : ?>
<div class="widget widget_ads_320" id="marketing_box">
<div class="ads320">
<div class="ads-content">
<?php
while ( $query_advertisements->have_posts() ) : $query_advertisements->the_post();
$adv_image = getUploadedImageSrc();
$adv_url = get_post_meta( get_the_ID(), \'adv_url\', true );
echo \'<a href="\' . $adv_url . \'"><img src="\' . $adv_image . \'" alt="\' . get_the_title() . \'" /></a>\';
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>
<?php else : ?>
<p>Error retrieving advertisements! Please try again later.<br><br></p>
<?php endif; ?>