我创建了名为“products”的自定义帖子类型和名为“product_category”的自定义分类。对于此自定义帖子类型,我还创建了一个Cpt-Template文件“single-products.php”,用于显示单个帖子和分类 - 模板“taxonomy-product_category”用于显示特定分类的所有帖子。
我使用the_permalink()来获取各个帖子的网址,当您点击帖子时,您会直接跳到“single-product.php”页面。我设法让所有这一切工作,找到单个帖子的cpt-template单页,但由于某种原因,我的wp_query不会从“single-product.php”页面运行。它将获得的唯一内容是帖子中的标题,编辑器和缩略图图像内容,不会显示其他自定义字段。我在CPT中创建了一个带有一些自定义字段的元数据箱。这些字段的工作和显示应该来自taxonomy-archive-page,但不是单页,我不明白为什么?
这是我的代码。我很感激能得到一些帮助。提前谢谢。
======我的自定义帖子类型“产品”=======
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);
}
}
======自定义帖子类型模板“single-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
答案 0 :(得分:2)
干杯,您可以使用archive-products.php来实现存档。
Wordpress为您的单{post_typ} .php和您的档案提供WP_Query- {post-typ} .php
无需重新声明WP_Query:
Common.css
archive- {post-typ} .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 )),
));
&GT;
单一 - {后典型} .php 的示例:
<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile; endif;
get_footer();
未对代码段进行测试。希望我能帮到你
答案 1 :(得分:0)
我终于有了这个工作!我想知道Lukas R.在我的单页(single-products.php)中没有重新声明wp_query-class的解释。我无法使archive-products.php工作,但我已经为此目的使用了自定义分类存档(taxonomy-product_category)。我所要做的只是在循环中包含我的字段
<?php get_header(); ?>
<main id="main" class="site-main" role="main">
<?php
// Start the loop
while ( have_posts() ) : the_post();
$products_intro = get_post_meta($post->ID, 'products_intro');
$products_desc = get_post_meta($post->ID, 'products_desc');
echo '<h1>';
the_title();
echo '</h1>';
the_post_thumbnail();
the_content();
// Custom Metabox-fields
$productsIntro = $products_intro[0] ? '<p class="products-title padd-bottom">'.$products_intro[0].'</p>' : '';
echo isset($productsIntro) ? $productsIntro : '';
$productsdesc = $products_desc[0] ? '<p class="products-title padd-bottom">'.$products_desc[0].'</p>' : '';
echo isset($productsdesc) ? $productsdesc : '';
// Advanced Custom Fields
the_field('innehall');
// End the loop.
endwhile;
?>
</main>
<?php get_footer(); ?>