我已經創建了一個名爲「產品」和自定義分類名爲「PRODUCT_CATEGORY」爲了這個自定義後類型我還創建了一個CPT-模板文件「單一產品自定義文章類型。 PHP「用於顯示個人帖子和Taxonomy-Template」taxonomy-product_category「,用於顯示我的特定分類標準的所有帖子。自定義類型後單頁wp_query將無法正常工作
我使用the_permalink()來獲取個別職位的URL,當你點擊後,你直接跳到「單product.php」 -page。我設法讓這一切工作,尋找CPT-模板單頁的單篇文章,但由於某種原因,我的wp_query將不會從「單product.php」 -page運行。它會抓住的唯一東西是帖子的標題,編輯器和縮略圖圖像內容,不會顯示其他自定義字段。我在我的CPT中創建了一些帶有一些自定義字段的metabox。這些領域的工作和顯示,他們應該從分類 - 歸檔頁面,但不是從單頁面,我不明白爲什麼?
這是我的代碼。我很欣賞在這方面得到一些幫助。提前致謝。
======我的自定義文章類型 「產品」 =======
function create_taxonomy_products() {
$labels = array(
'name' => _x('Produktkategorier', 'taxonomy singular name', 'products'),
'singular_name' => _x('Produktkategori', 'taxonomy singular name', 'products'),
'search_items' => __('Sök Produktkategori', 'products'),
'all_items' => __('Alla Produktkategorier', 'products'),
'parent_item' => __('Förälder Produktkategori', 'products'),
'parent_item_colon' => __('Förälder Produktkategori:', 'products'),
'edit_item' => __('Ändra Produktkategori', 'products'),
'update_item' => __('Uppdatera Produktkategori', 'products'),
'add_new_item' => __('Lägg till ny Produktkategori', 'products'),
'new_item_name' => __('Nytt Produktkategorinamn', 'products'),
'menu_name' => __('Produktkategori', 'products'),
'not_found' => __('Inga Produktkategorier hittade.', 'products'),
'view_item' => __('Se Produktkategorier', 'products'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array('slug' => 'produkter'),
);
register_taxonomy(
// Name of our Taxonomy
'product_category', 'products', $args);
}
add_action('init', 'create_taxonomy_products', 0);
function register_custom_post_type_products() {
$labels = array(
'name' => _x('Produkter', 'post type general name', 'products'),
'singular_name' => _x('Produkt', 'post type singular name', 'products'),
'menu_name' => _x('Produkter', 'admin menu', 'products'),
'name_admin_bar' => _x('Produkter', 'add new on admin bar', 'products'),
'add_new' => _x('Lägg till nya Produkter', 'Produkter', 'products'),
'add_new_item' => __('Lägg till ny Produkt', 'products'),
'new_item' => __('Nya Produkter', 'products'),
'edit_item' => __('Ändra Produkter', 'products'),
'view_item' => __('Se Produkter', 'products'),
'all_items' => __('Alla Produkter', 'products'),
'search_items' => __('Sök Produkter', 'products'),
'parent_item_colon' => __('Förälder Produkter:', 'products'),
'not_found' => __('Ingen Produkter hittad.', 'products'),
'not_found_in_trash' => __('Ingen Produkt hittad i papperskorgen.', 'products'),
'featured_image' => __('Produktbild', 'products'),
'set_featured_image' => __('Lägg till Produktbild', 'products'),
'remove_featured_image' => __('Ta bort Produktbild', 'products'),
'use_featured_image' => __('Använd Produktbild', 'products'),
);
$features = array(
'title',
'thumbnail',
'editor',
);
$args = array (
'labels' => $labels,
'public' => true,
'supports' => $features,
'has_archive' => false,
'menu_icon' => 'dashicons-networking',
'taxonomies' => array(
'product_category',
),
);
// Name of our Custom Post Type
register_post_type('products', $args);
}
add_action('init', 'register_custom_post_type_products');
function create_meta_box_products() {
add_meta_box(
'products_metabox',
'Produkter:',
'create_metabox_products',
'products',
'normal',
'default'
);
}
function products_metabox($post) {
echo 'Info here';
}
add_action('add_meta_boxes', 'create_meta_box_products');
function create_metabox_products($post) {
?>
<form action="" method="post">
<?php
wp_nonce_field('kk_metabox_nonce', 'kk_nonce');
$products_name = get_post_meta($post->ID, 'products_name', true);
$products_desc = get_post_meta($post->ID, 'products_desc', true);
?>
<p>
<span><i>Type in name of product</i></span><br/>
<input type="text" name="products_name" value="<?php echo esc_attr($products_name); ?>" placeholder="..." />
</p>
<p>
<span><i>Type in description of product</i></span><br/>
<input type="text" name="products_desc" value="<?php echo esc_attr($products_desc); ?>" placeholder="..." />
</p>
</form>
<?php
}
add_action('save_post', 'save_meta_products_name');
function save_meta_products_name($post_id) {
if(!isset($_POST['kk_nonce']) ||
!wp_verify_nonce($_POST['kk_nonce'],
'kk_metabox_nonce')) return;
if(isset($_POST['products_name'])) {
$new_value_products_name = ($_POST['products_name']);
update_post_meta($post_id, 'products_name', $new_value_products_name);
}
}
add_action('save_post', 'save_meta_products_desc');
function save_meta_products_desc($post_id) {
if(!isset($_POST['kk_nonce']) ||
!wp_verify_nonce($_POST['kk_nonce'],
'kk_metabox_nonce')) return;
if(isset($_POST['products_desc'])) {
$new_value_products_desc = ($_POST['products_desc']);
update_post_meta($post_id, 'products_desc', $new_value_products_desc);
}
}
======自定義文章類型模板 「單product.php」 == =====
if (have_posts()) {
?>
<div class="container">
<div class="row">
<?php
global $wp_query;
$term = $wp_query->get_queried_object();
$post_type = get_post_type($post->ID);
$wp_query = new WP_Query(array(
'post_type' => 'products',
'posts_per_page' => -1, // -1 = show all posts
'tax_query' => array(array('taxonomy' => 'product_category', 'field' => 'id', 'terms' => $term->term_id)),
));
?>
<?php
while($wp_query->have_posts()) {
$wp_query->the_post();
$products_name = get_post_meta($post->ID, 'products_name');
$products_desc = get_post_meta($post->ID, 'products_desc');
?>
<article class="products-col col-12 collapse entry-content">
<div class="col-12">
<?php
$productsName = $products_name[0] ? '<h2>'.$products_name[0].'</h2>' : '';
echo isset($productsName) ? $productsName : '';
$productsdesc = $products_desc[0] ? '<p>'.$products_desc[0].'</p>' : '';
echo isset($productsdesc) ? $productsdesc : '';
the_post_thumbnail();
the_content();
?>
</div>
</article>
<?php
} // while
wp_reset_postdata();
?>
</div> <!-- .row -->
</div> <!-- .container -->
<?php
} // if