【问题标题】:Wordpress: Custom post type single page shows index page insteadWordpress:自定义帖子类型单页显示索引页
【发布时间】:2017-02-06 04:10:59
【问题描述】:

我已经尝试制作我的第一个 wordpress 主题,但我的博客的单页遇到了一些问题。 [我的博客页面][1]

这是我创建的自定义帖子类型,名为 blog_post。 奇怪的是,这似乎仅在它位于我的站点 ex 的子层上时才会发生。 {url}/blog_post/ 这里的页面看起来就像我的索引页面。

但在我的工作页面上,我的单​​个页面没有任何问题,并且它们的永久链接没有 {url}/work_post/ 附加到它们。 [我的工作页面][2]

我使用 functions.php 文件创建了我的单页的特殊样式

define(SINGLE_PATH, TEMPLATEPATH . '/single');

/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');

/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;

/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :

if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';

elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
/*                                                                                                                              <-------- if no single-cat page is found the return the single.php file 
*/
elseif(file_exists(SINGLE_PATH . '/single.php'))
return SINGLE_PATH . '/single.php';

endforeach;
}

我希望有人能理解我的烂摊子:D

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    我自己找到了答案,只需删除对functions.php的添加,然后也为我的工作页面制作自定义帖子。

    然后我为我想要的每个 slug 制作单 {slug}.php 页面:D 大家都很开心

    【讨论】:

      【解决方案2】:

      您需要在自定义帖子类型中添加“public => true”。同时创建单{custom-post-type}.php 文件

      代码:

      $args = array(
              'labels'            => $labels,
              'public'            => true,
              'show_ui'           => true,
              'show_in_menu'      => true,
              'capability_type'   => 'post',
              'heirachical'       => false,
              'menu_position'     => 26,
              'has_archive'       => true,
              'menu_icon'         => 'dashicons-products',
              'supports'          => array( 'title', 'editor','thumbnail' ),
              'query_var'         => true,
              'capability_type'   => 'post',
              'rewrite'           => true,
          ); 
      

      例如:假设您创建了自定义帖子类型说“产品”,那么您需要在主题目录中创建 single-product.php 文件。

      注意:保存您的永久链接设置。

      【讨论】:

        【解决方案3】:

        对我来说,将'publicly_queryable' =&gt; true 添加到注册 post_type 的参数中很有效。我最初必须设置为false

        【讨论】:

          猜你喜欢
          • 2017-10-14
          • 1970-01-01
          • 1970-01-01
          • 2019-05-08
          • 1970-01-01
          • 2015-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多