Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何自定义wordpress内部函数,如相邻的\u post\u link()_Wordpress_Customization - Fatal编程技术网

如何自定义wordpress内部函数,如相邻的\u post\u link()

如何自定义wordpress内部函数,如相邻的\u post\u link(),wordpress,customization,Wordpress,Customization,我需要自定义WordPress上的上一个帖子链接()和下一个帖子链接() 举个例子,我想把permalinks(比如“你需要学习的前五大编程语言”)缩减为“前五大编程…” 负责创建链接的函数是位于wp的邻接的post\u link()包含/link template.php的下一个post\u link()和上一个post\u link()函数都带有自定义选项。阅读并了解该函数的可接受参数后,测试是否可以将php函数传递给该选项,如substr() “%link”和“%title”是文章链接

我需要自定义WordPress上的
上一个帖子链接()
下一个帖子链接()

举个例子,我想把permalinks(比如“你需要学习的前五大编程语言”)缩减为“前五大编程…”

负责创建链接
的函数是位于
wp的邻接的post\u link()
包含/link template.php

下一个post\u link()
上一个post\u link()
函数都带有自定义选项。阅读并了解该函数的可接受参数后,测试是否可以将php函数传递给该选项,如substr()


“%link”和“%title”是文章链接和标题的短代码


让我们知道它是否有效

要为帖子创建自定义相邻链接,我可以使用过滤器挂钩next\u post\u链接和previos\u post\u链接

在functions.php中:

function shrink_previous_post_link($format, $link){
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = true;
    $link='%title';
    $format='« %link';


    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

    if ( !$post )
        return;

    $title = $post->post_title;

    if ( empty($post->post_title) )
        $title = $previous ? __('Previous Post') : __('Next Post');

    $rel = $previous ? 'prev' : 'next';

    //Save the original title
    $original_title = $title;

    //create short title, if needed
    if (strlen($title)>40){
        $first_part = substr($title, 0, 23);
        $last_part = substr($title, -17);
        $title = $first_part."...".$last_part;
    }   

    $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
    $link = str_replace('%title', $title, $link);   
    $link = $string . $link . '</a>';

    $format = str_replace('%link', $link, $format);

    echo $format;   
}

function shrink_next_post_link($format, $link){
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = false;
    $link='%title';
    $format='%link &raquo;';

    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

    if ( !$post )
        return;

    $title = $post->post_title;

    if ( empty($post->post_title) )
        $title = $previous ? __('Previous Post') : __('Next Post');

    $rel = $previous ? 'prev' : 'next';

    //Save the original title
    $original_title = $title;

    //create short title, if needed
    if (strlen($title)>40){
        $first_part = substr($title, 0, 23);
        $last_part = substr($title, -17);
        $title = $first_part."...".$last_part;
    }   

    $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
    $link = str_replace('%title', $title, $link);   
    $link = $string . $link . '</a>';

    $format = str_replace('%link', $link, $format);

    echo $format;   
}

add_filter('next_post_link', 'shrink_next_post_link',10,2);
add_filter('previous_post_link', 'shrink_previous_post_link',10,2);
函数收缩\u前一个\u后一个链接($format,$link){
$in_same_cat=false;
$excluded_categories='';
$previous=true;
$link='%title';
$format='laquo;%link';
如果($previous&&is_attachment())
$post=&get\u post($GLOBALS['post']->post\u父项);
其他的
$post=获取相邻的帖子($in_same_cat,$excluded_categories,$previous);
如果(!$post)
返回;
$title=$post->post\u title;
if(空($post->post_title))
$title=$previous?_uu(‘上一篇文章’):_uu(‘下一篇文章’);
$rel=$previous?'prev':'next';
//保存原始标题
$original_title=$title;
//如果需要,创建简短标题
如果(斯特伦($title)>40){
$first_part=substr($title,0,23);
$last_part=substr($title,-17);
$title=$first_part.“…”$last_part;
}   
$string='';
$format=str_replace('%link',$link,$format);
echo$格式;
}
函数shrink\u next\u post\u链接($format,$link){
$in_same_cat=false;
$excluded_categories='';
$previous=假;
$link='%title';
$format='%link»;';
如果($previous&&is_attachment())
$post=&get\u post($GLOBALS['post']->post\u父项);
其他的
$post=获取相邻的帖子($in_same_cat,$excluded_categories,$previous);
如果(!$post)
返回;
$title=$post->post\u title;
if(空($post->post_title))
$title=$previous?_uu(‘上一篇文章’):_uu(‘下一篇文章’);
$rel=$previous?'prev':'next';
//保存原始标题
$original_title=$title;
//如果需要,创建简短标题
如果(斯特伦($title)>40){
$first_part=substr($title,0,23);
$last_part=substr($title,-17);
$title=$first_part.“…”$last_part;
}   
$string='';
$format=str_replace('%link',$link,$format);
echo$格式;
}
添加过滤器(“下一个帖子链接”,“收缩下一个帖子链接”,10,2);
添加过滤器(“前一个后一个链接”,“收缩前一个后一个链接”,10,2);

这就是我需要做的。谢谢

好吧,我需要限制文章页面上下一篇和上一篇文章链接的字符数,以避免出现错误-参见示例:Hey kevtrout!谢谢重播。你的想法不管用,但帮助我自己弄明白了。首先,substr是不可能的,因为wordpress代码不执行函数的参数。但是仔细看代码,我看到了一个我利用它的钩子。所以,我将在下面回答这个问题,这样每个人都能从中受益。
function shrink_previous_post_link($format, $link){
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = true;
    $link='%title';
    $format='&laquo; %link';


    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

    if ( !$post )
        return;

    $title = $post->post_title;

    if ( empty($post->post_title) )
        $title = $previous ? __('Previous Post') : __('Next Post');

    $rel = $previous ? 'prev' : 'next';

    //Save the original title
    $original_title = $title;

    //create short title, if needed
    if (strlen($title)>40){
        $first_part = substr($title, 0, 23);
        $last_part = substr($title, -17);
        $title = $first_part."...".$last_part;
    }   

    $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
    $link = str_replace('%title', $title, $link);   
    $link = $string . $link . '</a>';

    $format = str_replace('%link', $link, $format);

    echo $format;   
}

function shrink_next_post_link($format, $link){
    $in_same_cat = false;
    $excluded_categories = '';
    $previous = false;
    $link='%title';
    $format='%link &raquo;';

    if ( $previous && is_attachment() )
        $post = & get_post($GLOBALS['post']->post_parent);
    else
        $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

    if ( !$post )
        return;

    $title = $post->post_title;

    if ( empty($post->post_title) )
        $title = $previous ? __('Previous Post') : __('Next Post');

    $rel = $previous ? 'prev' : 'next';

    //Save the original title
    $original_title = $title;

    //create short title, if needed
    if (strlen($title)>40){
        $first_part = substr($title, 0, 23);
        $last_part = substr($title, -17);
        $title = $first_part."...".$last_part;
    }   

    $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
    $link = str_replace('%title', $title, $link);   
    $link = $string . $link . '</a>';

    $format = str_replace('%link', $link, $format);

    echo $format;   
}

add_filter('next_post_link', 'shrink_next_post_link',10,2);
add_filter('previous_post_link', 'shrink_previous_post_link',10,2);