这似乎是个愚蠢的问题。但是,我想不出来:(。
我需要在主页上显示按钮,该按钮指向自定义post\\u类型的归档URL(archive-{post\\u type}.php)。我该怎么做?
这似乎是个愚蠢的问题。但是,我想不出来:(。
我需要在主页上显示按钮,该按钮指向自定义post\\u类型的归档URL(archive-{post\\u type}.php)。我该怎么做?
你好@Silent:
事实证明,WordPress 3.1中有一个函数,它完全可以实现您想要的功能,并命名为get_post_type_archive_link()
; 下面是您如何称呼它的(假设一个名为\'product\'
):
<a href="<?php echo get_post_type_archive_link(\'product\'); ?>">Products</a>
下面是我在发现WordPress确实为这个用例内置了一个函数之前的回答。get_archive_link()
您可以这样调用它(假设一个名为\'product\'
):<a href="<?php echo get_archive_link(\'product\'); ?>">Products</a>
下面是源代码,您可以将其放入主题的function.php
文件或中的.php
您可能正在编写的插件的文件:if (!function_exists(\'get_archive_link\')) {
function get_archive_link( $post_type ) {
global $wp_post_types;
$archive_link = false;
if (isset($wp_post_types[$post_type])) {
$wp_post_type = $wp_post_types[$post_type];
if ($wp_post_type->publicly_queryable)
if ($wp_post_type->has_archive && $wp_post_type->has_archive!==true)
$slug = $wp_post_type->has_archive;
else if (isset($wp_post_type->rewrite[\'slug\']))
$slug = $wp_post_type->rewrite[\'slug\'];
else
$slug = $post_type;
$archive_link = get_option( \'siteurl\' ) . "/{$slug}/";
}
return apply_filters( \'archive_link\', $archive_link, $post_type );
}
}
事实上,我在周末研究了这个确切的逻辑,虽然我还不能百分之百确定WordPress可能看到的所有用例的逻辑顺序都是正确的,尽管它可能适用于任何特定的站点。这也是一个很好的建议,可以通过trac 我想今晚晚些时候我会这么做。
注册post类型时,可以使用“has\\u archive”参数将字符串作为slug传递,并确保还将rewrite设置为true或数组,但不设置为false,然后您的CPT归档URL将为http://www.YOURDOMAIN.com/has_archive_slug 例如
如果在register\\u post\\u类型中设置,例如:
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => \'product\',
\'capability_type\' => \'post\',
\'has_archive\' => \'products\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'author\',\'thumbnail\',\'excerpt\',\'comments\')
);
register_post_type(\'product\',$args);
那么您的单个url是:http://www.YOURDOMAIN.com/product/postName您的存档url为:http://www.YOURDOMAIN.com/products/