UPDATE (polylang plugin):
这是原始函数的修订版本,这次用于
polylang plugin:
//assume polylang as multilingual plugin
function get_link_by_template($template,$lang_slug = null){
global $wpdb;
$joins = "";
$wh = "";
if($lang_slug){
$joins = " INNER JOIN $wpdb->term_relationships tr ON meta.post_id = tr.object_id
INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id ";
$wh = " AND t.slug = %s";
}
$pages = $wpdb->get_results($wpdb->prepare(
"SELECT post_id
FROM $wpdb->postmeta meta " . $joins . "
WHERE meta.meta_key = %s AND meta.meta_value= %s" . $wh,
\'_wp_page_template\',
$template,
$lang_slug
));
if($pages && isset($pages[0])){
$p = $pages[0];
echo get_permalink($p->post_id);
}
}//get_link_by_template
usage:
//no language
get_link_by_template(\'contacts.php\');
//current language
get_link_by_template(\'contacts.php\',pll_current_language());
//manual language
get_link_by_template(\'contacts.php\',\'it\');
pll\\u current\\u language()返回当前语言。看见
polylang codex.
<小时>
OLD (for wpml):
我添加了另一个答案,以解决链接问题。我制作了另一个函数,用于“is\\u page\\u template”方式:
//assume wpml as multilingual plugin
function get_link_by_template($template,$check_lang = false){
global $wpdb;
//if there\'s the need of adding lang vars on the URL
$lang_join_string = (!$check_lang) ? "" : " left join ". $wpdb->prefix ."icl_translations tr on meta.post_id = tr.element_id ";
$lang_where_string = (!$check_lang) ? "" : " AND tr.language_code = \'" . ICL_LANGUAGE_CODE . "\'";
$pages = $wpdb->get_results($wpdb->prepare(
"SELECT post_id
FROM $wpdb->postmeta meta" . $lang_join_string . "
WHERE meta.meta_key = \'_wp_page_template\' AND meta.meta_value= %s" . $lang_where_string,
$template));
if($pages && isset($pages[0])){
$p = $pages[0];
echo get_permalink($p->post_id);
}
}//get_link_by_template