我一直在尝试在我的自定义帖子类型中显示特定子类别中的所有帖子,名为' Training'。子类别被称为' spotlight-on-events' &安培;我一直试图显示放在类别中的每个帖子,但我找不到。我尝试过很多不同的东西。检查了功能,我无法看到哪些应该影响到这一点。 可以任何WordPress专家采取循环和放大看看问题可能会出现什么问题。
以下是查询:
<?php
$date_args_side = array(
'category_name' => 'spotlight-on-events',
'post_type' => 'training',
'posts_per_page' => 10,
);
$date_query_side = new WP_Query( $date_args_side );
?>
<aside>
<div class="small-12 large-3 large-offset-1 columns webinar-event" style="padding:0;overflow:hidden;">
<h2>Spotlight On Events</h2>
<hr>
<?php if( $date_query_side->have_posts() ): ?>
<?php while( $date_query_side->have_posts() ) : $date_query_side->the_post(); ?>
<div class="full-width">
<div class="small-12 large-12 columns" style="padding:0">
<?php
$attachment_id = get_post_thumbnail_id(); // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id,'full' ); // returns an array
?>
<img src="<?php bloginfo('template_directory'); ?>/imageResizer/imgresize.php?src=<?php echo $image_attributes[0]; ?>&h=500&w=750&q=95"> </div>
<div class="small-12 large-12 columns white upcoming-events-side"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="event-date">
<?php the_title(); ?>
</a> <span class="alignright date-updates">
<?php $dateformatstring = "l d F"; $unixtimestamp = strtotime(get_field('date_picker'));echo date_i18n($dateformatstring, $unixtimestamp); ?>
</span>
<p><?php echo get_excerpt(180); ?></p>
<a href="<?php the_field('webinar_url'); ?>" class="call2actionbuttonred">Read More</a> </div>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php _e('There are no upcoming Spotlight Events','example'); ?>
<?php endif; wp_reset_query(); ?>
</div>
</aside>
以下是自定义帖子类型的functions.php设置:
function training_taxonomy() {
register_taxonomy(
'training', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'training', //post type name
array(
'hierarchical' => true,
'label' => 'Training Categories', //Display name
'query_var' => true
)
);
}
add_action( 'init', 'register_themepost2', 20 );
function register_themepost2() {
$labels = array(
'name' => _x( 'Training', 'catchthemes_custom_post','catchthemes' ),
'singular_name' => _x( 'Training', 'catchthemes_custom_post', 'catchthemes' ),
'add_new' => _x( 'Add New', 'catchthemes_custom_post', 'catchthemes' ),
'add_new_item' => _x( 'Add New Training', 'catchthemes_custom_post', 'catchthemes' ),
'edit_item' => _x( 'Edit Training', 'catchthemes_custom_post', 'catchthemes' ),
'new_item' => _x( 'New Training', 'catchthemes_custom_post', 'catchthemes' ),
'view_item' => _x( 'View Training', 'catchthemes_custom_post', 'catchthemes' ),
'search_items' => _x( 'Search Training', 'catchthemes_custom_post', 'catchthemes' ),
'not_found' => _x( 'No Training found', 'catchthemes_custom_post', 'catchthemes' ),
'not_found_in_trash' => _x( 'No Training found in Trash', 'catchthemes_custom_post', 'catchthemes' ),
'parent_item_colon' => _x( 'Parent Training:', 'catchthemes_custom_post', 'catchthemes' ),
'menu_name' => _x( 'Webinars & Events', 'catchthemes_custom_post', 'catchthemes' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Training',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'public' => true,
'rewrite' => array('slug' => 'training','with_front' => false),
'has_archive' => true,
'capability_type' => 'post'
);
register_post_type( 'training', $args );//max 20 charachter cannot contain capital letters and spaces
}
add_action( 'init', 'training_taxonomy');
正如您从查询中看到的那样,我已将其剥离,只是为了使其正常工作,但
谢谢
答案 0 :(得分:1)
对于其他想知道如何执行此操作的人,我将上述查询更改为:
<?php
$date_args_side = array(
'post_type' => 'training',
'tax_query' => array(
array(
'taxonomy' => 'training',
'field' => 'slug',
'terms' => 'spotlight-on-events',
)
),
'posts_per_page' => 10,
);
$date_query_side = new WP_Query( $date_args_side );
?>
现在这是一种享受 - 谢谢Pieter Goosen