Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify a theme-bundled widget in child theme?

I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here

theme folder/example theme/lib/activity/plugin.php.

Here is the exact code:

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

Now please tell me how to remove the nofollow tag from this code using the child theme?

like image 483
Aamir Usman Avatar asked Oct 06 '22 19:10

Aamir Usman


1 Answers

Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:

Filtering the output of WordPress widgets

The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:

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;
}
like image 93
webaware Avatar answered Oct 10 '22 02:10

webaware