自定义类型后单页wp_query将无法正常工作
我已经创建了一个名为“产品”和自定义分类名为“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
欢呼声,您可以使用archive-products.php来实现存档。
WordPress的为您提供WP_Query你的单{} post_typ .php然后你archive- {-典型后} .PHP
有无需重新声明的WP_Query:
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)), ));
示例archive- {-典型交} .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();
>
示例单{-典型交} .PHP:
<?php get_header(); ?>
<main id="main" class="site-main" role="main">
<?php
// Start the loop
while (have_posts()) : the_post();
echo '<h1>';
the_title();
echo '</h1>';
echo '<a href="';
the_permalink();
echo '">That\'s me! -> '; the_title();
echo '</a>';
the_content();
// End the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_footer(); ?>
的代码片段,未经测试。希望我可以帮助你
我终于得到这个工作!我想通过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(); ?>