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

标签 wordpress 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 标记?

最佳答案

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

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;
}

关于wordpress - 如何修改子主题中主题捆绑的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13691148/

相关文章:

ios - 将 UITextField 输入文本翻译成西类牙语运行时

wordpress - 在 Widgets Wordpress 中保存复选框组

php - 没有插件的 WordPress 移动主题切换器(用户代理和 Cookie)

php - 通过标签收集自定义帖子类型

css - 无法获取字体以在 Wordpress 上使用我的字体

javascript - 将鼠标悬停在帖子链接上时,我可以使用帖子缩略图作为光标图像吗? (文字出版社)

php - 如何解析和抓取WordPress的内容

php - WordPress:rewind_posts()、wp_reset_postdata() 和 wp_reset_query() 之间的区别

wordpress-theming - 我应该在单个 wordpress 模板中使用 $post = Timber::query_post() 还是 $post = new TimberPost() ?

Wordpress 单帖子类型模板不显示帖子