Link wordpress 中的 ID url
Link ID in wordpress url
目前我正在做一个 wordpress 网站,但我偶然发现了一个小问题。
我正在使用 wp_page_menu
制作导航栏以访问我的其他页面。现在它指引我到 www.mysite.com/sport
,但我需要它指引我到 www.mysite.com/sport#header
或 www.mysite.com/sport/#header
。
有人知道我该怎么做吗?
我尝试使用不同的插件,或者更改 slug 或永久链接,但这并没有给我想要的结果。
感谢大家的帮助!
好的。这是你的代码。我只是覆盖了 wordpress 的默认 walker。请关注此代码中的文本#header。
如果您担心这段代码。因此,您可以转到文件:...wp-includes/post-template.php 第 1238 行。代码相同,但我在 get_permalink().
之后添加了#header
希望有所帮助。
class WPSE_HasHeader_Custom_Walker extends Walker_Page {
function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if( isset( $args['pages_with_children'][ $page->ID ] ) )
$css_class[] = 'page_item_has_children';
if ( !empty($current_page) ) {
$_current_page = get_post( $current_page );
if ( in_array( $page->ID, $_current_page->ancestors ) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
if ( '' === $page->post_title )
$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
/** CHANGE LINK HERE **/
$output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '#header">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
if ( !empty($show_date) ) {
if ( 'modified' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
}
}
wp_list_pages(array(
'sort_column' => 'menu_order',
'title_li' => '',
'echo' => 1,
'walker' => new WPSE_HasHeader_Custom_Walker()
));
已更新:
第二种方式:
感谢纯粹的发展。我认为这与他的想法相同,但在您的情况下要简单得多。像这样:
$link = wp_list_pages(array(
'sort_column' => 'menu_order',
'title_li' => '',
'echo' => 0,
));
echo preg_replace('/<a(.*)href="([^"]*)"(.*)>/', '<ahref="#header">', $link);
Christian 的回答很好,这让我开始思考其他方法,尤其是那些允许您按最初要求使用 wp_page_menu()
函数的方法。所以这是使用过滤器的另一种方法。
在 wp_page_menu()
调用之前或 functions.php
中添加此函数
function wp_list_pages_addhash($output) {
$dom = new DOMDocument;
$dom->loadHTML($output);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$link->setAttribute('href', $link->getAttribute('href') . '#header');
}
$output = $dom->saveHTML();
return $output;
}
add_filter('wp_list_pages', 'wp_list_pages_addhash');
然后使用您最初在主题中的 wp_page_menu 调用:
wp_page_menu();
解释:它在wp_list_pages
的输出中找到每个link的每个href属性,并在末尾添加'#header'。 wp_page_menu
依次使用 wp_list_pages
创建输出。
目前我正在做一个 wordpress 网站,但我偶然发现了一个小问题。
我正在使用 wp_page_menu
制作导航栏以访问我的其他页面。现在它指引我到 www.mysite.com/sport
,但我需要它指引我到 www.mysite.com/sport#header
或 www.mysite.com/sport/#header
。
有人知道我该怎么做吗?
我尝试使用不同的插件,或者更改 slug 或永久链接,但这并没有给我想要的结果。
感谢大家的帮助!
好的。这是你的代码。我只是覆盖了 wordpress 的默认 walker。请关注此代码中的文本#header。
如果您担心这段代码。因此,您可以转到文件:...wp-includes/post-template.php 第 1238 行。代码相同,但我在 get_permalink().
之后添加了#header希望有所帮助。
class WPSE_HasHeader_Custom_Walker extends Walker_Page {
function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if( isset( $args['pages_with_children'][ $page->ID ] ) )
$css_class[] = 'page_item_has_children';
if ( !empty($current_page) ) {
$_current_page = get_post( $current_page );
if ( in_array( $page->ID, $_current_page->ancestors ) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
if ( '' === $page->post_title )
$page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
/** CHANGE LINK HERE **/
$output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '#header">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
if ( !empty($show_date) ) {
if ( 'modified' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
}
}
wp_list_pages(array(
'sort_column' => 'menu_order',
'title_li' => '',
'echo' => 1,
'walker' => new WPSE_HasHeader_Custom_Walker()
));
已更新:
第二种方式: 感谢纯粹的发展。我认为这与他的想法相同,但在您的情况下要简单得多。像这样:
$link = wp_list_pages(array(
'sort_column' => 'menu_order',
'title_li' => '',
'echo' => 0,
));
echo preg_replace('/<a(.*)href="([^"]*)"(.*)>/', '<ahref="#header">', $link);
Christian 的回答很好,这让我开始思考其他方法,尤其是那些允许您按最初要求使用 wp_page_menu()
函数的方法。所以这是使用过滤器的另一种方法。
在 wp_page_menu()
调用之前或 functions.php
function wp_list_pages_addhash($output) {
$dom = new DOMDocument;
$dom->loadHTML($output);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$link->setAttribute('href', $link->getAttribute('href') . '#header');
}
$output = $dom->saveHTML();
return $output;
}
add_filter('wp_list_pages', 'wp_list_pages_addhash');
然后使用您最初在主题中的 wp_page_menu 调用:
wp_page_menu();
解释:它在wp_list_pages
的输出中找到每个link的每个href属性,并在末尾添加'#header'。 wp_page_menu
依次使用 wp_list_pages
创建输出。