如何获取 WordPress 各类页面的链接

如何获取 WordPress 各类页面的链接,第1张

获取文章或页面链接

直接输出文章或页面的链接:

1

<php the_permalink(); >

返回文章或页面的链接,以供调用:

1

get_permalink();

可以使用 echo 输出,结果和直接使用 the_permalink() 一样:

1

<php echo get_permalink(); >

获取存档页面链接

function get_current_archive_link( $paged = true ) {

$link = false;

if ( is_front_page() ) {

$link = home_url( '/' );

} else if ( is_home() && "page" == get_option('show_on_front') ) {

$link = get_permalink( get_option( 'page_for_posts' ) );

} else if ( is_tax() || is_tag() || is_category() ) {

$term = get_queried_object();

$link = get_term_link( $term, $term->taxonomy );

} else if ( is_post_type_archive() ) {

$link = get_post_type_archive_link( get_post_type() );

} else if ( is_author() ) {

$link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );

} else if ( is_archive() ) {

if ( is_date() ) {

if ( is_day() ) {

$link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );

} else if ( is_month() ) {

$link = get_month_link( get_query_var('year'), get_query_var('monthnum') );

} else if ( is_year() ) {

$link = get_year_link( get_query_var('year') );

}

}

}

if ( $paged && $link && get_query_var('paged') > 1 ) {

global $wp_rewrite;

if ( !$wp_rewrite->using_permalinks() ) {

$link = add_query_arg( 'paged', get_query_var('paged'), $link );

} else {

$link = user_trailingslashit( trailingslashit( $link ) trailingslashit( $wp_rewrite->pagination_base ) get_query_var('paged'), 'archive' );

}

}

return $link;

}

该函数可以输出首页、分类法(自定义分类法、标签、分类)、自定义文章类型的存档页面、作者存档页面、日期存档页面 的链接,包含分页。

获取当前页面链接

如果你不想判断页面类型,只想输出当前页面的链接,可以使用下面的代码:

<php

global $wp;

$current_url = home_url(add_query_arg(array(),$wp->request));

echo $current_url;

>

$("a")attr("src"); //获取地址

$("a")text(); //获取text

如果获取别的网页一样可以用,获取到的是一个集合。处理一下就行

几种获取html字符串中所有a链接地址代码

$ss = "<a href='1gif'>d</a>adfxx<a href="dirhtml">dir</a>";

print_r(gethref($ss));

    

function gethref($temp){

        preg_match_all('/<a(: |\t|\r|\n)href=['"](+)['"](:(: |\t|\r|\n)+)>(+)</a>/sim',$temp,$a); 

        return $a[1];

    }

//下面所有a连接

$htm = preg_replace( "@<a()</a>@is","$1",$ss);

print_r($htm);

//比较全的提取url连接地址

 

 $url="";

 $html=file_get_contents($url,"r");

 preg_match_all ("/(<a)()(href=)([^>])(>)([^<])(<)([^>])(>)/", $html, $matches);

 for ($i=0;$i<count($matches[0]);$i++)

 {

  echo "|||"$matches[2][$i]"||"$matches[3][$i]$matches[4][$i]"||"$matches[6][$i]"||"$matches[8][$i]"<br>";

 }//提取文章字符串中所有链接地址

preg_match_all('/(<=href=")[wd:/]/',$str,$fstr);

想获取网站中的所有超链接,使用的是php snoopy类

$sourceURL = $url;

$snoopy->fetchlinks($sourceURL);

$content = $snoopy->results;

以上就是关于如何获取 WordPress 各类页面的链接全部的内容,包括:如何获取 WordPress 各类页面的链接、如何获取网页中所有的超链接地址 以及链接innerText、几种获取html字符串中所有a链接地址代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9307251.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存