是否在更多阅读链接中包含帖子标题?

时间:2013-05-28 作者:speedypancake

正在尝试在我的摘录readmore链接中包含帖子标题。我读过这方面的法典,但我的主题文件要复杂得多,所以我需要一些专家的帮助。。。

从函数中提取。php:*编辑以显示更多代码。。。。。。。。。

function theme_get_content($args = array()) {

$more_tag = theme_get_array_value($args, \'more_tag\', __(\'Continue Bob <span class="meta-nav">&rarr;</span>\', THEME_NS));
theme_ob_start();
the_content($more_tag);
$content = theme_ob_get_clean();
return $content . wp_link_pages(array(
            \'before\' => \'<p><span class="page-navi-outer page-navi-caption"><span class="page-navi-inner">\' . __(\'Pages\', THEME_NS) . \': </span></span>\',
            \'after\' => \'</p>\',
            \'link_before\' => \'<span class="page-navi-outer"><span class="page-navi-inner">\',
            \'link_after\' => \'</span></span>\',
            \'echo\' => 0
        ));
}

function theme_get_excerpt($args = array()) {
global $post;
$more_tag = theme_get_array_value($args, \'more_tag\', __(\'My Words...<span class="meta-nav">&rarr;</span>\', THEME_NS));
$auto = theme_get_array_value($args, \'auto\', theme_get_option(\'theme_metadata_excerpt_auto\'));
$all_words = theme_get_array_value($args, \'all_words\', theme_get_option(\'theme_metadata_excerpt_words\'));
$min_remainder = theme_get_array_value($args, \'min_remainder\', theme_get_option(\'theme_metadata_excerpt_min_remainder\'));
$allowed_tags = theme_get_array_value($args, \'allowed_tags\',
    (theme_get_option(\'theme_metadata_excerpt_use_tag_filter\')
        ? explode(\',\',str_replace(\' \', \'\', theme_get_option(\'theme_metadata_excerpt_allowed_tags\')))
        : null));
$perma_link = get_permalink($post->ID);
$more_token = \'%%theme_more%%\';
$show_more_tag = false;
$tag_disbalance = false;
if (post_password_required($post)) {
    return get_the_excerpt();
}
if ($auto && has_excerpt($post->ID)) {
    $excerpt = get_the_excerpt();

    $show_more_tag = theme_strlen($post->post_content) > 0;
} else {
    $excerpt = get_the_content($more_token);
    // hack for badly written plugins
    theme_ob_start();
    echo apply_filters(\'the_content\', $excerpt);
    $excerpt = theme_ob_get_clean();
    global $multipage;
    if ($multipage && theme_strpos($excerpt, $more_token) === false) {
        $show_more_tag = true;
    }
    if (theme_is_empty_html($excerpt))
        return $excerpt;
    if ($allowed_tags !== null) {
        $allowed_tags = \'<\' . implode(\'><\', $allowed_tags) . \'>\';
        $excerpt = strip_tags($excerpt, $allowed_tags);
    }
    if (theme_strpos($excerpt, $more_token) !== false) {
        $excerpt = str_replace($more_token, $more_tag, $excerpt);
    } elseif ($auto && is_numeric($all_words)) {
        $token = "%theme_tag_token%";
        $content_parts = explode($token, str_replace(array(\'<\', \'>\'), array($token . \'<\', \'>\' . $token), $excerpt));
        $content = array();
        $word_count = 0;
        foreach ($content_parts as $part) {
            if (theme_strpos($part, \'<\') !== false || theme_strpos($part, \'>\') !== false) {
                $content[] = array(\'type\' => \'tag\', \'content\' => $part);
            } else {
                $all_chunks = preg_split(\'/([\\s])/u\', $part, -1, PREG_SPLIT_DELIM_CAPTURE);
                foreach ($all_chunks as $chunk) {
                    if (\'\' != trim($chunk)) {
                        $content[] = array(\'type\' => \'word\', \'content\' => $chunk);
                        $word_count += 1;
                    } elseif ($chunk != \'\') {
                        $content[] = array(\'type\' => \'space\', \'content\' => $chunk);
                    }
                }
            }
        }

        if (($all_words < $word_count) && ($all_words + $min_remainder) <= $word_count) {
            $show_more_tag = true;
            $tag_disbalance = true;
            $current_count = 0;
            $excerpt = \'\';
            foreach ($content as $node) {
                if ($node[\'type\'] == \'word\') {
                    $current_count++;
                }
                $excerpt .= $node[\'content\'];
                if ($current_count == $all_words) {
                    break;
                }
            }
            $excerpt .= \'&hellip;\'; // ...
        }
    }
}


if ($show_more_tag) {
    $excerpt = $excerpt . \' <a class="more-link" href="\' . $perma_link . \'">\' . $more_tag . \' </a>\';
}

if ($tag_disbalance) {
    $excerpt = force_balance_tags($excerpt);
}
return $excerpt;
}

如何在readmore permalink中包含帖子标题???谢谢

2 个回复
最合适的回答,由SO网友:Adam 整理而成

如果下面的代码实际上是控制您的“阅读更多”链接的代码,那么您也许可以使用下面的代码;

if ($show_more_tag) {
    global $post;
    $excerpt = $excerpt . \' <a class="more-link" href="\' . $perma_link . \'">\' . get_the_title($post->ID) . \' </a>\';
}
我已经声明global $post; 同样,以防上面对条件语句的调用是在初始$post 显示在您的第一个代码片段中;因为您还没有提供完整的代码进行评估。

但是试试上面的方法,看看是否有任何效果,然后告诉我们。

SO网友:s_ha_dum

您可以使用the_content_more_link 滤器

function the_content_more_link_wpse_100990($more) {
  global $post;
  $pattern = \'|>([^<]+)<|\';
  return preg_replace($pattern,">$1{$post->post_title} <",$more,1);
}   
add_filter(\'the_content_more_link\',\'the_content_more_link_wpse_100990\');
如果我要把它投入生产,我可能会做一些错误检查,但这就是我的想法。

结束