【问题标题】:cant show details of a post of custom-post-type -wordpress无法显示自定义帖子类型 -wordpress 的帖子的详细信息
【发布时间】:2013-12-03 08:15:52
【问题描述】:

我有这样的自定义帖子类型

add_action( 'init', 'create_post_type_feedback' );

function create_post_type_feedback() {
    register_post_type( 'testimonial',
        array(
            'labels' => array(
                'name' => __( 'Feedbacks' ),
                'singular_name' => __( 'Feedback' )
            ),
            'supports' => array('title','editor','thumbnail','custom-fields'),
            'public' => true,
            'has_archive' => true,
        )
    );
}

在一个页面中,我显示了推荐列表并使用函数 get_permalink() 获取超链接。要显示推荐帖子的详细信息,我有“single-testimonial.php”,但每当我点击查看推荐的详细信息时,它会将我重定向到“..../testimonial/postname”之类的链接。虽然有单证。 php ,它显示了 404.php 的内容。

如何显示自定义帖子类型的帖子的详细信息?

【问题讨论】:

    标签: php wordpress routing


    【解决方案1】:

    要使单个帖子页面正常工作,您必须在注册自定义帖子类型函数中添加一些内容...即:

    'public' => true,                            // yes you want it to be public?
    'show_ui' => true,                           // you want it shown in the admin area
    'show_in_menu' => true,                      // show it in menus etc..
    'rewrite' => array('slug' => 'testimonial'), // this is the permalink structure 
    'show_in_nav_menus' => true,                 // show in navigation menus
    'publicly_queryable' => true,                // include this in searches
    'query_var' => true,                         // do you want to pass values?
    'capability_type' => 'post',                 // is it like a post or page?
    'menu_position' => 25,                       // the position on the admin menu!
    

    添加蛞蝓应该可以解决问题, 注意:一旦设置了 slug,转到设置-> 永久链接设置回默认值,然后设置回 postname,刷新重写规则,这样 wordpress 就知道它在那里并且可以工作了!这应该可以解决问题,您不需要将任何模板代码添加到任何文件中,

    只需添加 single-testimonial.php 它应该可以工作或 page-testimonial.php 取决于您希望如何设置..

    马蒂

    【讨论】:

      【解决方案2】:

      在您的 single-testimonial.php 中,确保您在顶部有这一行:

      // Template Name: Single Testimonial template
      

      这是为了让 Wordpress 知道这是一个模板文件,并允许您在创建推荐帖子时从“页面属性”部分中选择它。

      其次,使用get_post_permalink() 函数代替get_permalink()。这是why

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        • 2016-12-29
        • 1970-01-01
        • 1970-01-01
        • 2011-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多