【问题标题】:Wordpress Archives Widget - Customize html outputWordpress 档案小工具 - 自定义 html 输出
【发布时间】:2011-02-15 21:05:42
【问题描述】:

我似乎仍然反对 wordpress。我将小部件“存档”添加到我的侧边栏,再一次,html 输出是废话,它基本上具有以下结构:

<li><a href="somelink">text</a> - (# of posts)</li>

我想把它变成:

<li><a href="somelink">text <small># of posts</small></a>

然而,与插件不同的是,我无法在 wordpress 社区建议/提到的 php 页面中找到创建 html 输出的行,即 functions.php、widgets.php 和 default-widgets.php

我已经用谷歌搜索了所有可能的关键字组合,但找不到相关的内容。

感谢所有帮助

问候

G.坎波斯

【问题讨论】:

    标签: php wordpress widget customization


    【解决方案1】:

    查看general-template.php。两个函数 wp_get_archives 和 get_archives_link。您必须破解 wp_get_archives 才能更改 $text 中加载的内容。帖子计数被加载到 $after 变量中,该变量位于 get_archives_link 中的链接之外。而不是这个:

    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    if ( $show_post_count )
       $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
    

    类似这样的:

    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    if ( $show_post_count )
       $text= $text.'&nbsp;<small>'.$arcresult->posts.'</small>';
    

    这仅适用于每月存档。您必须对 Yearly、Weekly 和 Daily 块进行修改。

    编辑:从链接标题中排除&lt;small&gt; 元素的最简单方法是将其加载到每个块中的单独变量中,然后将其传递到修改后的get_archives_link。在上面的示例中,在 $text 加载之后,只需将该值加载到 $title 中:

    $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
    $title = $text;
    if ( $show_post_count )
       $text= $text.'&nbsp;<small>'.$arcresult->posts.'</small>';
    $output .= get_archives_link($url, $text, $format, $before, $after, $title);
    

    然后修改get_archives_link:

    function get_archives_link($url, $text, $format = 'html', $before = '', $after = '', $title = '') {
        $text = wptexturize($text);
    
        if($title == '')
            $title = $text;
    
        $title_text = esc_attr($title);
        $url = esc_url($url);
    
        if ('link' == $format)
            $link_html = "\t<link rel='archives' title='$title_text' href='$url' />\n";
        elseif ('option' == $format)
            $link_html = "\t<option value='$url'>$before $text $after</option>\n";
        elseif ('html' == $format)
            $link_html = "\t<li>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
        else // custom
            $link_html = "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
    
        $link_html = apply_filters( "get_archives_link", $link_html );
    
        return $link_html;
    }
    

    【讨论】:

    【解决方案2】:

    将此代码添加到您的主题 functions.php 文件中,它将在 span 标记内包装发布存档计数。在下面的代码示例中,我将计数包装在 span 标签中,您可以根据需要添加或修改它。

    function wrap_archive_count($links) {
           $links = str_replace('</a>&nbsp;(', '<span class="archive-count">', $links);
           $links = str_replace(')', '</span></a>', $links);
           return $links;
    }
    add_filter('get_archives_link', 'wrap_archive_count');
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多