single-product.php 未打开

single-product.php not opening

我创建了一组页面。在第一页 product.php 中显示了所有自定义类别(标题和图像)。如果用户点击它,它就会移动到 taxonomy-product_categories 页面,其中显示了特定类别的产品。现在我希望如果用户点击产品然后它会转到 single-product.php 页面。

代码在这里

<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
    'post_type' => 'products', // your post type
    'posts_per_page' => 999,
    'orderby' => 'id',
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_categories', // your taxonomy
            'field' => 'slug',
            'terms' => $slug
        )
    )
);
?>
<div id="main-wrap">
    <div class="container">
        <?php
        $counter = 1;
        $tax_post_qry = new WP_Query($tax_post_args);
        if ($tax_post_qry->have_posts()) {
            while ($tax_post_qry->have_posts()) {
                $tax_post_qry->the_post();
                $the_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $type);
                //var_dump($the_url);


                if ($counter % 4 == 0) {
                    echo '<div class="row product-gallery">';
                }
                ?>
                <div class="col-sm-3">
                    <div class="product-warp">
                        <div class="product">
                            <a href="#"><img src="<?php echo $the_url[0] ?>" title="" alt=""></a>
                        </div>
                        <div class="product-name">
                            <h5>
                                <a href="">
                                    <?php echo get_the_title();
                                    ; ?>
                                </a>
                            </h5>
                        </div>
                    </div>
                </div>
                <?php
                if ($counter % 4 == 0) {
                    echo '</div>';
                }
                ?>

                <?php
            }
        }
        ?>
    </div>
</div>
<?php get_footer(); ?>

这是用户点击的地方然后应该去single-product.php

<a href="#"><img src="<?php echo $the_url[0] ?>" title="" 

请有人一步步指导我

请尝试

'post_type' => 'products'

您的 Post 类型是“产品”。 而 wordpress 规则是“single-{post_type}.php”。 你制作“single-products.php”并删除“single-product.php

https://codex.wordpress.org/Post_Type_Templates

如果你的post_type => products那么你的文件名一定是这样的single-products.php

读这个article

以及注意事项(如果页面还没有打开。意味着给出404错误)

  1. admin panel
  2. settings
  3. permalinks

首先将永久链接保存为普通链接并刷新您的网站主页。

然后将永久链接保存为 post_name 并再次刷新您的主页

希望对您有所帮助。