WordPress:post_type->;重写[';slug';]返回post#U类型,而不是slug

WordPress:post_type->;重写[';slug';]返回post#U类型,而不是slug,wordpress,Wordpress,在我的应用程序中,我需要在admin dashboard中创建一个小部件,该小部件将显示所有post_类型的一部分,与每个类型的帖子数量相关 为了完成上述操作,我在functions.php文件中添加了以下代码块: add_action('wp_dashboard_setup', 'tp_post_counts_reference'); function tp_post_counts_reference() { global $wp_meta_boxes;

在我的应用程序中,我需要在admin dashboard中创建一个小部件,该小部件将显示所有
post_类型的一部分
,与每个类型的帖子数量相关

为了完成上述操作,我在
functions.php
文件中添加了以下代码块:

add_action('wp_dashboard_setup', 'tp_post_counts_reference');

    function tp_post_counts_reference() {
        global $wp_meta_boxes;
        wp_add_dashboard_widget('custom_help_widget', 'Posts in all post types', 'custom_dashboard_help');
    }

    function custom_dashboard_help() {
        $types = get_post_types();
        foreach( $types as $type )
        {
            if($type != 'travelog' && $type != 'package_tour' && $type != 'hotel-info') continue;
            $typeobj = get_post_type_object( $type );

            echo '<a href="/' . $typeobj->rewrite['slug'] . '">' . $typeobj->labels->name . '</a>: ' . wp_count_posts( $type )->publish . '<br />';
        }
    }

:6

:11
当我期望他们输出:

<a href="/travelogs">Travelog</a>: 6
:6

:11
请告诉我我做错了什么:(


注意:我的WP版本是4.7.5,我建议您使用内置的WordPress功能,而不是手动构建您的帖子类型URL

看起来是这样的:

function custom_dashboard_help() {
    $types = get_post_types();

    // Alternate method for testing if custom type.
    $custom_types = array( 'travelog', 'package_tour', 'hotel-info' );
    foreach( $types as $type ) {
        // If not a custom post type, don't render the link
        if( ! in_array( $type, $custom_types ) ) {
            continue;
        }

        $typeobj = get_post_type_object( $type );

        // Use get_post_type_archive_link function to get URL
        echo '<a href="' . get_post_type_archive_link( $type ) . '">' . $typeobj->labels->name . '</a>: ' . wp_count_posts( $type )->publish . '<br />';
    }
}
函数自定义仪表板帮助(){
$types=get_post_types();
//测试是否为自定义类型的替代方法。
$custom_types=数组('travelog','package_tour','hotel info');
foreach($type作为$type){
//如果不是自定义帖子类型,则不呈现链接
如果(!in_数组($type,$custom_类型)){
继续;
}
$typeobj=get\u post\u type\u对象($type);
//使用get\u post\u type\u archive\u link函数获取URL
echo':'.wp_count_posts($type)->发布。
; } }
谢谢你的回复。我尝试了你的解决方案,但它返回了所有帖子类型的
home\u url()
。似乎根据,它返回了false!我在我的CPT中添加了
has\u archive=>true;
()。然后停用并重新激活了插件。但所有帖子类型仍然指向
home\u url()
。出了什么问题?您好,请忽略我之前的消息。我的CPT声明中有一个输入错误。您的解决方案确实有效!谢谢:)我已将其转化为一个答案,以帮助未来的访问者。如果它解决了你的问题,请考虑接受答案。
<a href="/hotel-info">Hotel</a>: 11
<a href="/travelogs">Travelog</a>: 6
<a href="/hotels">Hotel</a>: 11
function custom_dashboard_help() {
    $types = get_post_types();

    // Alternate method for testing if custom type.
    $custom_types = array( 'travelog', 'package_tour', 'hotel-info' );
    foreach( $types as $type ) {
        // If not a custom post type, don't render the link
        if( ! in_array( $type, $custom_types ) ) {
            continue;
        }

        $typeobj = get_post_type_object( $type );

        // Use get_post_type_archive_link function to get URL
        echo '<a href="' . get_post_type_archive_link( $type ) . '">' . $typeobj->labels->name . '</a>: ' . wp_count_posts( $type )->publish . '<br />';
    }
}