如何添加单一类别帖子模板

时间:2019-04-08 21:27:43

标签: php wordpress underscores-wp

我正在开发wordpress主题,我需要根据类别开发几种单一的帖子类型。

现在我有2个类别:

  • 项目(slug =项目)
  • 照片(sl =照片)

understrap主题中,我创建了一个名为single的文件夹,并在该文件夹中放入了single-cat-projects.phpsingle-cat-photos.php

然后在function.php中添加以下过滤器:

  add_filter( 'single_template', function ( $template )
{
    $post = get_queried_object();
    $path = 'single/';

    $templates = [];

    if ( 'post' !== $post->post_type ) {
        $templates[] = $path . 'single-' . $post->post_type . '-' . $post->post_name . '.php';
        $templates[] = $path . 'single-' . $post->post_type . '.php';
    }

    if ( 'post' === $post->post_type ) {
        $categories = get_the_category( $post->ID );

        if ( $categories ) {
            foreach ( $categories as $category ) {
                $templates[] = $path . 'single-cat-' . $category->name . '.php';
            }
        }
    }

    $templates[] = $path . 'single.php';
    $templates[] = $path . 'default.php';
    $templates[] = 'index.php';

    $template = locate_template( $templates );

    return $template;
});

在localhost中,它运作良好,但是当我传递到cpanel时,并不会假设这是单个页面...我复制了所有文件项目,并生成了要导入phpmyadmin的数据库。

我不知道这里会发生什么。我还已经检查了单个文件夹的权限。

我该如何解决这种情况?

谢谢

0 个答案:

没有答案
相关问题