如何在子主题中修改主题捆绑的小部件?

时间:2012-12-03 20:28:59

标签: wordpress wordpress-plugin wordpress-theming

我想从侧边栏中显示的最新帖子中删除nofollow代码。我发现将rel = nofollow标签添加到最新帖子的代码位于此处

主题文件夹/示例主题/ lib / activity / plugin.php。

以下是确切的代码:

private function get_latest_posts( $post_count ) {

    // Get the latest posts
    $latest_posts = get_posts(
        array(
            'numberposts'   => $post_count,
            'order'         => 'desc',
            'orderby'       => 'date'
        )
    );

    // Create the markup for the listing
    $html = '<div class="tab-pane" id="recent">';
        $html .= '<ul class="latest-posts">';

        if( count( $latest_posts ) > 0 ) {

            foreach( $latest_posts as $post ) {

                $html .= '<li class="clearfix">';

                    // Add the small featured image
                    if( has_post_thumbnail( $post->ID ) ) {
                        $html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
                            $html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
                        $html .= '</a>';
                    } // end if

                    $html .='<div class="latest-meta">';    

                        // Add the title
                        $html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
                            $html .= get_the_title( $post->ID );
                        $html .= '</a>';

                        // Add date posted
                        // If there's no title, then we need to turn the date into the link
                        if( strlen( get_the_title( $post->ID ) ) == 0 ) {
                            $html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
                        } // end if

                        $html .= '<span class="latest-date">';
                            $html .= get_the_time( get_option( 'date_format' ), $post->ID );
                        $html .= '</span>';

                        // Close the anchor 
                        if(strlen( get_the_title( $post->ID ) ) == 0 ) {
                            $html .= '</a>';
                        } // end if

                    $html .='</div>';

                $html .= '</li>';
            } // end foreach

        } else {

            $html .= '<li>';
                $html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
            $html .= '</li>';

        } // end if/else

        $html .= '</ul>';
    $html .= '</div>';

    return $html;

} // end get_latest_posts

现在请告诉我如何使用子主题从此代码中删除nofollow标记?

2 个答案:

答案 0 :(得分:3)

由于您可以控制子主题,因此您可以包含调用以显示该窗口小部件的窗口小部件区域,其中包含抓取输出,对其执行正则表达式搜索/替换以及输出结果的内容。我最近写了一篇关于这篇文章的博客文章:

Filtering the output of WordPress widgets

基础知识是你有一个用你自己的函数替换dynamic_sidebar()的函数,如下所示:

function theme_dynamic_sidebar($index = 1) {
    // capture output from the widgets
    ob_start();
    $result = dynamic_sidebar($index);
    $out = ob_get_clean();
    // do regex search/replace on $out
    echo $out;
    return $result;
}

答案 1 :(得分:1)

似乎你运气不好。

主题作者提供了private function而没有filter hooks

您可以尝试覆盖include('path/to/plugin.php');并包含您自己的修改版本。