【发布时间】:2017-05-11 14:15:04
【问题描述】:
您好,我在 Wordpress 中创建了自己的自定义帖子类型,以包含可以通过主题文件调用的项目。
我不熟悉创建自己的主题。我目前在我的single.php文件中使用以下代码,根据博文的类别调用相关文章。
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
在我的新帖子类型“项目”中,我想调用相关项目。我假设这将是非常相似的代码,除了我需要停止它寻找帖子而是寻找我的项目。
这是我的新帖子类型代码:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
为了查找 bw_projects 而不再查找“帖子”,我需要在我的第一个代码 sn-p 中进行哪些更改。我尝试自己玩弄并更改某些行,但我引起了更多问题并停止了页面加载。这是否正确,我可以使用相同的代码,稍微改变一下,还是我需要完全不同的东西?
提前致谢。
【问题讨论】:
标签: php html arrays wordpress blogs