• Resolved piterkaillada

    (@thebea58)


    Hi Héctor!

    First of all, thank you very much for this fantastic plugin. It’s a killer!

    In my last steps customizing your plugin, I’m trying to display a custom taxonomy tag along with the standard “category” taxonomy, in different lines right under the post title. I’ve realized that I can only have one selected in the Stats Tag settings. Is there any by-pass for this?

    Thank you very much in advance!

    Off-topic:
    I realized about something and I wanted to ask you if you could help me with it.
    In my GTMetrix report I get this message and it’s always about the images this plugin uses.
    “The following resources have identical contents, but are served from different URLs. Serve these resources from a consistent URL to save 1 request(s)…”
    and it shows two URLs, one for the location of the file in …/uploads/ and the second for that same file located within uploads/wordpress-popular-posts/.

    Is there anything you would suggest to avoid the duplicate content?

    Your time and help is very much appreciated. Thanks!

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 25 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @thebea58,

    In my last steps customizing your plugin, I’m trying to display a custom taxonomy tag along with the standard “category” taxonomy, in different lines right under the post title. I’ve realized that I can only have one selected in the Stats Tag settings. Is there any by-pass for this?

    There is, but it requires a bit of coding: WPP filter hooks.

    Basically, any features that the plugin doesn’t offer out-of-the-box (such as displaying different taxonomies simultaneously) will require hooking into WPP to customize its HTML output to whatever fits your needs.

    Is there anything you would suggest to avoid the duplicate content?

    Well, yes. By default, the plugin creates thumbnails using the original ones as a base so of course there will be duplicates 😛

    You can workaround this by having the widget use the “stock” thumbnails instead (Widgets > WordPress Popular Posts > Display post thumbnail > Use predefined size). You will have to create a new thumbnail size in your theme if the current choices don’t match the size you need, though (and you’ll have to regenerate the thumbnails afterwards as well).

    P.S.: I knew I had seen this site somewhere. It has progressed quite a bit since I last saw it, good work!

    Thread Starter piterkaillada

    (@thebea58)

    Yes, I asked for your help in other regard a couple of months ago, in Github if I’m not wrong. Thanks! 🙂

    And thanks as well for such a quick and detailed reply, like always!!!

    I’ll try everything you suggested and let you know.

    Thread Starter piterkaillada

    (@thebea58)

    Hi again, Héctor

    1. Custom thumbnail size created and in use in the plugin 🙂

    2.

    will require hooking into WPP to customize its HTML output to whatever fits your needs

    I’ve spent hours reading all the articles and tutorials and documentation provided in your plugin pages and linked in them, but this stuff is beyond my skills…

    If I’m not wrong I should create a filter with get_the_terms function, something like what is shown here, perhaps? Or with get_the_term_list?

    I’m at a loss. Even if I had the right function ready, I don’t know how to create the association with the plugin (I mean, what to write the code so the filter only affects WPP). I guess that wpp_custom_html is the way to go, but I’m just lost about it all. Any help, instructions or links to learn more would be very much appreciated.

    Thank you very much in advance, again!!!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey there!

    I’ve spent hours reading all the articles and tutorials and documentation provided in your plugin pages and linked in them, but this stuff is beyond my skills…

    I know the feeling. Don’t worry, we all have been there at some point.

    Alright, try this:

    /**
     * Builds custom HTML for the WordPress Popular Posts widget.
     *
     * @param	array	$posts_array
     * @param	array	$instance
     * @return	string
     */
    function my_custom_popular_posts_html_list( $posts_array, $instance ){
    
        // Opening list tag
        $output = '<ul class="rpwe-ul">';
    
        // Loop the array of popular posts objects
        foreach( $posts_array as $popular_post ) {
    
            $stats = array(); // placeholder for the stats tag
    
            // Category option checked
            if ( $instance['stats_tag']['category'] ) {
    
                $categories = get_the_category( $popular_post->id );
                $post_cat_tag = '';
    
                foreach( $categories as $category ) {
                    $post_cat_tag .= '<a href="' . get_category_link( $category->term_id ) . '">' . $category->cat_name . '</a> &diamond;';
                }
    
                // Remove last "diamond" symbol
                $post_cat_tag = rtrim( $post_cat_tag, "  &diamond;" );
    
                $stats[] = '<span class="rpwe-category">' . $post_cat_tag . '</span>';
    
            }
    
            // Get taxonomy
            $taxonomies = get_the_terms( $popular_post->id, 'YOUR_TAXONOMY_SLUG_HERE' );
    
            if ( $taxonomies && ! is_wp_error($taxonomies) ) {
    
                $post_tax_tag = '';
    
                foreach( $taxonomies as $taxonomy ) {
                    $post_tax_tag .= '<a href="' . get_term_link( $taxonomy->term_id ) . '">' . $taxonomy->name . '</a> &diamond;';
                }
    
                // Remove last "diamond" symbol
                $post_tax_tag = rtrim( $post_tax_tag, "  &diamond;" );
    
                $stats[] = '<span class="rpwe-taxonomy">' . $post_tax_tag . '</span>';
    
            }
    
            // Build stats tag
            if ( !empty( $stats ) ) {
                $stats = join( ' ', $stats );
            }
    
            // Opening post tag
            $output .= "<li>";
    
            // Thumbnail
            $output .= ( has_post_thumbnail( $popular_post->id ) ) ? '<a href="' . get_permalink( $popular_post->id ) . '" class="rpwe-img">' . get_the_post_thumbnail( $popular_post->id, 'YOUR_POST_THUMBNAIL_SIZE' ) . '</a>' : '';
            // Post title
            $output .= '<h3 class="rpwe-title"><a href="' . get_permalink( $popular_post->id ) . '" title="' . esc_attr( $popular_post->title ) . '">" . $popular_post->title . "</a></h3>";
            // Taxonomy tags
            $output .= $stats;
    
            $output .= "</li>";
    
        }
    
        $output .= '</ul>';
    
        return $output;
    
    }
    add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );

    There are a couple of changes you need to make to the code before you can use it on your site so make sure to read it first.

    Thread Starter piterkaillada

    (@thebea58)

    Héctor, this is beyond any expectations, beyond amazing.

    I really appreciate that you take your time to give this high quality help and you being always so kind and nice. I wish there was more people like you.

    I’m gonna read the code and try to make it work (you made it really easy for me). Highly appreciated, many thanks! 😀

    Thread Starter piterkaillada

    (@thebea58)

    Hi again, Héctor

    I reviewed the code, made the couple of changes needed and tried it, via Code Snippets plugin and pasting it in my functions.php. In both cases, I get the same error. This is the message generated in Code Snippets:

    The code snippet you are trying to save produced a fatal error on line 72:
    syntax error, unexpected ‘/’

    Line 72 is:$output .= '</ul>';

    It’s probably something simple, but again, I’m not there yet.

    Thank you one more time!

    Plugin Author Hector Cabrera

    (@hcabrera)

    That’s odd.

    Try adding it directly into your theme’s functions.php via Appeareance > Editor (if my memory serves me right, I’m not near my laptop right now).

    If it still doesn’t work after that do let me know and I’ll have a look as soon as I can.

    Thread Starter piterkaillada

    (@thebea58)

    Hi again and thank for the follow-up of the issue 🙂

    I added it at the end of my child’s theme functions.php. and got this message:

    Parse error: syntax error, unexpected ‘/’ in /home/thebea58/public_html/wp-content/themes/olsen-child/functions.php on line 111

    Line 111 has the same code than in the previous error message: $output .= '</ul>';

    Whenever you can look at it will be a good moment. Thank you so much.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Ah, my bad! There are a couple of missing single quotes in there (that’s what happens when you don’t test stuff before sharing it with the world).

    Please change:

    // Post title
    $output .= '<h3 class="rpwe-title"><a href="' . get_permalink( $popular_post->id ) . '" title="' . esc_attr( $popular_post->title ) . '">" . $popular_post->title . "</a></h3>";

    with:

    // Post title
    $output .= '<h3 class="rpwe-title"><a href="' . get_permalink( $popular_post->id ) . '" title="' . esc_attr( $popular_post->title ) . '">"' . $popular_post->title . '"</a></h3>"';

    That should take care of things 🙂

    Thread Starter piterkaillada

    (@thebea58)

    Hi again, Héctor.

    First of all, thank you very much for your kindness and solutions 😀

    Secondly, there’s still some things not working out properly, but I don’t want to take advantage of you, so just let me know if I’m asking too much, ok?

    That should take care of things 🙂

    That was effective, now the custom taxonomy is displayed, but at the same time:
    1. added a bullet point to the list
    2. Altered the image sizes. Now they’re as big as they can get in the column. When I edited the value of get_the_post_thumbnail, I used the registered value (1080, 706). Later, I changed it to (80, 52), which is the size I’d like (I registered it, FYI), but the images have been the same size all along since I used the code.
    3. It changed the styling of all the data displayed (could it be because the class “tags” have changed? see #4 below). I think I can customize everything with css, except the quotes wrapping the post title, that I’d like to get rid of.
    4. Out of curiosity: for all the class “tags” you used “rpwe” instead of “wpp”. Is there a reason for that? Any connection with Recent Posts Widget Extended plugin I also use?

    Would it be wise to uninstall the plugin and start everything from scratch?

    Well, in any case, thank you very very much for your time, interest, knowledge and solutions! 🙂

    PS: by the way, the diamond separator was a high class detail 😉

    Plugin Author Hector Cabrera

    (@hcabrera)

    There’s a Popular Posts list hidden in your home page that I used as a base to build the code I shared earlier. That list uses the rpwe class everywhere 😛 Change the class names to the right ones and you’re good to go.

    Out of curiosity: for all the class “tags” you used “rpwe” instead of “wpp”. Is there a reason for that? Any connection with Recent Posts Widget Extended plugin I also use?

    Ah, I guess this is where I got the class names from. The hidden list is also named “Popular Posts”, hence the confusion.

    Altered the image sizes. Now they’re as big as they can get in the column. When I edited the value of get_the_post_thumbnail, I used the registered value (1080, 706). Later, I changed it to (80, 52), which is the size I’d like (I registered it, FYI), but the images have been the same size all along since I used the code.

    • Are you using a caching plugin?
    • Did you regenerate the thumbnails after creating the new size (80×52)?

    Would it be wise to uninstall the plugin and start everything from scratch?

    No need to. All of your current issues are CSS-related, unless there’s another problem you haven’t mentioned yet?

    PS: by the way, the diamond separator was a high class detail 😉

    Well, thanks. I’m glad you liked it 🙂

    Thread Starter piterkaillada

    (@thebea58)

    Hi Héctor;

    I changed all the class tags to wpp.

    I deleted all cache and regenerated thumbnails and all the images are still as wide as their column. This is my thumbnail code:

    // Thumbnail
            $output .= ( has_post_thumbnail( $popular_post->id ) ) ? '<a href=' . get_permalink( $popular_post->id ) . ' class="wpp-img">' . get_the_post_thumbnail( $popular_post->id, '80, 52' ) . '</a>' : '';

    I tried different options for the thumbnail size value, but nothing worked. Changing the value through the widget controls in the Dashboard (tried a registered size of 45×45) didn’t work either.
    Despite the current look is pretty cool, I’d like to be able to edit the thumbnails size, if possible.

    All css can be easily done, I think. I don’t know how to remove the bullet point, though.

    One more time, huge thanks!

    Plugin Author Hector Cabrera

    (@hcabrera)

    get_the_post_thumbnail( $popular_post->id, '80, 52' )

    No, that’s not correct.

    The get_the_post_thumbnail() function accepts the following parameters:

    get_the_post_thumbnail(
        int|WP_Post $post = null,
        string|array $size = 'post-thumbnail',
        string|array $attr = ''
    )

    The second parameter has to be either an array of width & height (array(80, 52)) or the name of the thumbnail size as you registered it in your functions.php file (eg ‘my_popular_post_thumbnail’). So:

    get_the_post_thumbnail( $popular_post->id, array(80, 52) )

    or:

    get_the_post_thumbnail( $popular_post->id, 'my_popular_post_thumbnail' )

    will do.

    All css can be easily done, I think. I don’t know how to remove the bullet point, though.

    Easy. Use the list-style property:

    
    list-style: none;
    /* "none" tells the browser that the targeted list should not display any bullets */
    
    Thread Starter piterkaillada

    (@thebea58)

    No, that’s not correct.

    The get_the_post_thumbnail() function …

    Like I told you, I’m a total beginner, I’m learning as I go, and in most cases imitation is my best play. That’s why receiving the kind of support you are offering in every single of your messages is so great and so much appreciated!

    I’m glad to let you know that, it took me a while, but I managed to use css to style the widget to my complete liking and fitting the rest of the blog.

    Just FYI, I had to change all the selectors in wpp.css, because they all changed after I used your code.

    One last question: will all these modifications also apply if using the plugin via shortcode?

    Well, Héctor, like I said many times, huge, huge thanks. It might be something simple for you, but I couldn’t have done all this changes without your kind help, for which I am extremely grateful.Your plugin and you are great!

    Plugin Author Hector Cabrera

    (@hcabrera)

    I’m a total beginner, I’m learning as I go, and in most cases imitation is my best play. That’s why receiving the kind of support you are offering in every single of your messages is so great and so much appreciated!

    Don’t mention it. I also had people teach me all of this stuff when I was starting out so giving something back isn’t a big deal. Hopefully sometime in the future you’ll help out someone else too, and the chain will go on.

    I’m glad to let you know that, it took me a while, but I managed to use css to style the widget to my complete liking and fitting the rest of the blog.

    Good work! Keep it up!

    Just FYI, I had to change all the selectors in wpp.css, because they all changed after I used your code.

    You’ll want to move the modified wpp.css file into your theme’s folder. Otherwise future updates of the plugin will overwrite it with the “stock” one. WPP will then use that one instead.

    One last question: will all these modifications also apply if using the plugin via shortcode?

    Yep. The filter hooks will modify the output of all instances of the WPP list, including those generated via the [wpp] shortcode. Widgets, however, can display different outputs (eg. you might want to use a custom HTML structure for a widget and use the “stock” one for another) if you use the widget_id parameter (included in $instance) and a bit of coding logic 😛 Pseudo code example:

    my_function( $popular_posts, $instance ) {
      if ( isset($instance['widget_id']) && 'wpp-8' == $instance['widget_id'] ) {
        // return html output for widget wpp-8
      }
      else {
        // return html output for the rest of the widgets / shortcodes
      }
    }
Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Display custom taxonomy’ is closed to new replies.