wordpress 自定义 post 类型二十十七子主题
wordpress custom post type twenty seventeen child theme
朋友们,我使用 twentyseventeen 主题创建了一个子主题,我正在尝试自定义 post 来显示多个产品及其类别。我使用了以下代码,它可以很好地按照我想要的方式显示所有产品,但是当单击任何产品以打开产品详细信息时,它会显示 404 not found 错误。
请帮助我解决这个问题,在此先感谢。
这里是 function.php 文件中的代码
add_action( 'init', 'rmcc_create_post_type' );
function rmcc_create_post_type() { // clothes custom post type
// set up labels
$labels = array(
'name' => 'Products',
'singular_name' => 'Product',
'add_new' => 'Add New',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product',
'all_items' => 'All Products',
'view_item' => 'View Product',
'search_items' => 'Search Product',
'not_found' => 'No Product Found',
'not_found_in_trash' => 'No Product found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Products',
);
register_post_type(
'Products',
array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
'taxonomies' => array( 'post_tag', 'category' ),
'exclude_from_search' => true,
'capability_type' => 'post',
)
);
}
// SHORTCODE
add_shortcode( 'list-posts-basic', 'rmcc_post_listing_shortcode' );
function rmcc_post_listing_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'category' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => 'Products',
'category_name' => $category,
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<div class="gallery">
<div class="gallery-sortbar" id="portfolio-filter">
<a href="#all" title="">All</a>
<?php
/**Code for get terms*/
// no default values. using these as examples
$taxonomy = 'category';
$category_id = get_term_by('name',$category, $taxonomy);
$term_id = $category_id->term_id;
$child_categories = get_term_children( $term_id, $taxonomy );
for($i=0;$i<count($child_categories);$i++){
$category_name = get_term( $child_categories[$i], $taxonomy );
$catreal_name = $category_name->name;
$catfilterfull = str_replace(' ', '', $catreal_name);
echo '<a href="#'.$catfilterfull.'" title="'.$catfilter.'">'.$catreal_name.'</a>';
}
?>
</div>
<div class="gallery-viewer">
<div id="portfolio-wrapper">
<div id="portfolio-list" class="row">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="col-xs-12 col-sm-6 col-md-3 portfolio-item all
<?php
foreach((get_the_category()) as $catname) {
if ($catname->category_parent != '') {
$catfilter = ( $catname->cat_name != 'local' ) ? $catname->cat_name . '' : '';
$catfilterfull = str_replace(' ', '', $catfilter);
echo ' '.$catfilterfull;
} }
?>
">
<a href="<?php the_permalink(); ?>" class="thumbnail">
<?php the_post_thumbnail( array(250, 250) ); ?>
<div class="caption"><?php the_title(); ?></div></a>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<div class="clearboth"></div>
</div>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
添加新的自定义 post 类型时,您需要刷新永久链接。
转到管理中的设置 -> 永久链接并点击保存。
朋友们,我使用 twentyseventeen 主题创建了一个子主题,我正在尝试自定义 post 来显示多个产品及其类别。我使用了以下代码,它可以很好地按照我想要的方式显示所有产品,但是当单击任何产品以打开产品详细信息时,它会显示 404 not found 错误。 请帮助我解决这个问题,在此先感谢。
这里是 function.php 文件中的代码
add_action( 'init', 'rmcc_create_post_type' );
function rmcc_create_post_type() { // clothes custom post type
// set up labels
$labels = array(
'name' => 'Products',
'singular_name' => 'Product',
'add_new' => 'Add New',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product',
'all_items' => 'All Products',
'view_item' => 'View Product',
'search_items' => 'Search Product',
'not_found' => 'No Product Found',
'not_found_in_trash' => 'No Product found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Products',
);
register_post_type(
'Products',
array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
'taxonomies' => array( 'post_tag', 'category' ),
'exclude_from_search' => true,
'capability_type' => 'post',
)
);
}
// SHORTCODE
add_shortcode( 'list-posts-basic', 'rmcc_post_listing_shortcode' );
function rmcc_post_listing_shortcode( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'category' => '',
), $atts ) );
// define query parameters based on attributes
$options = array(
'post_type' => 'Products',
'category_name' => $category,
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<div class="gallery">
<div class="gallery-sortbar" id="portfolio-filter">
<a href="#all" title="">All</a>
<?php
/**Code for get terms*/
// no default values. using these as examples
$taxonomy = 'category';
$category_id = get_term_by('name',$category, $taxonomy);
$term_id = $category_id->term_id;
$child_categories = get_term_children( $term_id, $taxonomy );
for($i=0;$i<count($child_categories);$i++){
$category_name = get_term( $child_categories[$i], $taxonomy );
$catreal_name = $category_name->name;
$catfilterfull = str_replace(' ', '', $catreal_name);
echo '<a href="#'.$catfilterfull.'" title="'.$catfilter.'">'.$catreal_name.'</a>';
}
?>
</div>
<div class="gallery-viewer">
<div id="portfolio-wrapper">
<div id="portfolio-list" class="row">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="col-xs-12 col-sm-6 col-md-3 portfolio-item all
<?php
foreach((get_the_category()) as $catname) {
if ($catname->category_parent != '') {
$catfilter = ( $catname->cat_name != 'local' ) ? $catname->cat_name . '' : '';
$catfilterfull = str_replace(' ', '', $catfilter);
echo ' '.$catfilterfull;
} }
?>
">
<a href="<?php the_permalink(); ?>" class="thumbnail">
<?php the_post_thumbnail( array(250, 250) ); ?>
<div class="caption"><?php the_title(); ?></div></a>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<div class="clearboth"></div>
</div>
</div>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
添加新的自定义 post 类型时,您需要刷新永久链接。
转到管理中的设置 -> 永久链接并点击保存。