• Resolved George

    (@giorgos93)


    Hi, Hector! Just wanted to ask, if WPP plugin can replace YARPP plugin, by showing related posts at the end of each post (based on categories and posts’ names, for example)?

    Is it possible to do smth like that with the help of your plugin?: https://skrinshoter.ru/sWcFmsi395X

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

    (@hcabrera)

    Hi @giorgos93,

    Well, the two plugins fulfill different roles. YARPP is a plugin that -as you already know- displays a list of posts that are somehow connected to the current one (eg. from the same category) whereas WordPress Popular Posts lists popular posts. So I wouldn’t say WPP can replace YARPP. At least not completely.

    What you could do though is have WPP display popular posts that are on the same category as the current post, like this for example (although it could be better done.) That would kind of be similar to what YARPP does, I guess.

    Thread Starter George

    (@giorgos93)

    @hcabrera thanks for the answer! I use a classic editor, so I have [wpp] shortcode in sidebar for popular posts list. The solution, that you mentioned above: can it be implemented along with [wpp] shortcode? I want to use your solution at the end of each post, if it’s possible.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Yes, it can be implemented with the shortcode as well but I’m not near my computer right now. I’ll leave another comment with the solution once I get back.

    Thread Starter George

    (@giorgos93)

    thanks, @hcabrera , I’ll be waiting then!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Ok, I believe that something like this should work. Add the following code to your theme’s functions.php file and the [wpp] shortcode should automatically filter popular posts by the same category ID as the current post:


    /**
    * Modifies the [wpp] shortcode so it automatically filters
    * popular posts by the category assigned to the current post.
    *
    * @param array $out The output array of shortcode attributes.
    * @returns array $out The (modified) output array of shortcode attributes.
    */
    function wpp_shortcode_filter_by_current_category( $out ) {
    if ( is_single() ) {
    $post_id = get_queried_object_id();
    $post_category_IDs = wp_get_post_categories( $post_id, array( 'fields' => 'ids' ) );
    $out['cat'] = $post_category_IDs[0];
    }

    return $out;
    }
    add_filter( 'shortcode_atts_wpp', 'wpp_shortcode_filter_by_current_category', 1, 1 );

    I didn’t test it though so if you have any comments / questions please let me know.

    Thread Starter George

    (@giorgos93)

    @hcabrera I’ve just tested it, and it works, thanks! However, this code uses the main category of the post, and all of it’s subcategories (even if they are not used in a certain post). Is it possible to filter popular posts only by usedsubcategories of the post?

    And also, is it possible to exclude viewed post from the list? So it won’t show the same page, that you are on.

    And can I use [wpp] shortcode on the same page twice (in sidebar and at the end of each post)? If yes, then can I use different styles for them? Because I already set needed styles for sidebar, and I’ll need different styles at the end of each post.

    Thread Starter George

    (@giorgos93)

    While creating separate styles for second [wpp] shortcode (at the end of each post), I faced a problem, that I can’t fix for several hours. I made this shortcode:

    [wpp range='last30days' limit=6 post_type='post' thumbnail_width=185 thumbnail_height=140 wpp_start='<div class="wpp-custom-cards">' post_html='<div class="wpp-custom-card"><div class="wpp-custom-thumb">{thumb}</div><div class="wpp-custom-title"><a class="wpp-custom-title-link" href="{url}">{text_title}</a></div></div>' wpp_end='</div>']

    However, it created 6 posts, and each of them has the same link inside (URL of the same page that is opened at that moment). But in thumbnails all the links are included correctly. And the posts titles are shown correctly as well – only URLs inside them are incorrect. How can I fix it?

    And also: is there any way, how to filter posts not only by categories, but also by posts title? In other words, to show relevant posts by category and by similar title at the same time.

    Thread Starter George

    (@giorgos93)

    @hcabrera just want to make sure, that you didn’t miss my comments 🙂

    Plugin Author Hector Cabrera

    (@hcabrera)

    I did see your comments, I was just busy with work & life in general and only replied to other questions that I immediately knew the answer to without having to think too much 😛

    About your questions:

    Is it possible to filter popular posts only by used subcategories of the post?

    and:

    And also, is it possible to exclude viewed post from the list? So it won’t show the same page, that you are on.

    Yes to both. The code below has been updated so 1) we filter by child categories whenever possible, and 2) current post gets excluded from the list.

    /**
    * Modifies the [wpp] shortcode so it automatically filters
    * popular posts by the category assigned to the current post.
    *
    * @param array $out The output array of shortcode attributes.
    * @returns array $out The (modified) output array of shortcode attributes.
    */
    function wpp_shortcode_filter_by_current_category( $out ) {
    if ( is_single() && in_the_loop() ) {
    $post_id = get_queried_object_id();
    $post_category_IDs = wp_get_post_categories( $post_id, array( 'fields' => 'id=>parent' ) );

    /* Find the first child category ID so we can use it to filter posts */
    $child_category_ID = array_find_key(
    $post_category_IDs,
    function($parent_ID) {
    return $parent_ID > 0;
    }
    );

    /* This post isn't linked to a child category, fallback to the first ID */
    if ( ! $child_category_ID ) {
    $child_category_ID = array_key_first($post_category_IDs);
    }

    /* Get popular posts from this category */
    $out['cat'] = $child_category_ID;

    /* Exclude current post */
    $out['pid'] = $post_id;
    }

    return $out;
    }
    add_filter( 'shortcode_atts_wpp', 'wpp_shortcode_filter_by_current_category', 1, 1 );

    And about this question:

    And can I use [wpp] shortcode on the same page twice (in sidebar and at the end of each post)? If yes, then can I use different styles for them? Because I already set needed styles for sidebar, and I’ll need different styles at the end of each post.

    Yes, you can. You’re already using some custom HTML with your popular posts list, just make sure that both lists are using different CSS classes and style them accordingly.

    For example, let’s say that this is your after-post-content shortcode (shortened for brevity):

    [wpp ... wpp_start='<div class="wpp-custom-cards">' post_html='<div class="wpp-custom-card"><div class="wpp-custom-thumb">{thumb}</div><div class="wpp-custom-title"><a class="wpp-custom-title-link" href="{url}">{text_title}</a></div></div>' wpp_end='</div>']

    And this could be your sidebar one (shortened for brevity):

    [wpp ... wpp_start='<div class="wpp-sidebar-list">' post_html='<div class="wpp-sidebar-item"><div class="wpp-sidebar-thumb">{thumb}</div><div class="wpp-sidebar-title"><a class="wpp-sidebar-title-link" href="{url}">{text_title}</a></div></div>' wpp_end='</div>']

    However, it created 6 posts, and each of them has the same link inside (URL of the same page that is opened at that moment). But in thumbnails all the links are included correctly. And the posts titles are shown correctly as well – only URLs inside them are incorrect. How can I fix it?

    Sounds like you have some broken code on your end.

    I replicated your setup on my test site, one shortcode on the sidebar and one shortcode at the end of a post. The same shortcode as yours, not a single change. Everything worked as expected, no weird broken links as you reported.

    How are you adding the shortcode at the end of your posts exactly?

    And also: is there any way, how to filter posts not only by categories, but also by posts title? In other words, to show relevant posts by category and by similar title at the same time.

    No, there is not. As I said, WPP is not really a replacement for YARPP 😛

    Thread Starter George

    (@giorgos93)

    Hi, @hcabrera , thanks for the answer!

    Yes to both. The code below has been updated so 1) we filter by child categories whenever possible, and 2) current post gets excluded from the list.

    $out[‘pid’] = $post_id; did work for excluding current post! But if I add all the code, then it doesn’t filter by child categories whenever possible – instead it starts to filter by ALL popular posts, like it did without this code. Maybe it has mistakes?

    just make sure that both lists are using different CSS classes and style them accordingly.

    I did exactly that, but it didn’t help, unfortunately…

    How are you adding the shortcode at the end of your posts exactly?

    The first shorcode was put directly in Widgets (by using “HTML widget”). It works as excepted for many months:

    [wpp range=’last7days’ limit=5 post_type=’post’ thumbnail_width=75 thumbnail_height=75 stats_views=0 wpp_start='<ul class=”wpp-list wpp-cards-compact”>’ post_html='<li class=”{current_class}”>{thumb}<div class=”wpp-item-data”><div class=”taxonomies”></div>{title}</div>‘ wpp_end=’‘]

    The second (problematic) shortcode was put in “Ad Inserter” plugin (I chose there “Post type: Posts” setting and “Insert after content” setting):

    <div class=”wpp-custom-heading”>ЧИТАЙТЕ ТАКЖЕ:</div>
    [wpp range=’last30days’ limit=6 post_type=’post’ thumbnail_width=185 thumbnail_height=140 wpp_start='<div class=”wpp-custom-cards”>’ post_html='<div class=”wpp-custom-card”><div class=”wpp-custom-thumb”>{thumb}</div><div class=”wpp-custom-title”>{text_title}</div></div>’ wpp_end='</div>’]

    And also: is it possible to add target=”_blank” rel=”noopener” to the first shortcode, and “target=_self” to the second shortcode?

    Plugin Author Hector Cabrera

    (@hcabrera)

    Maybe it has mistakes?

    No, I don’t think that’s the case. I actually tested this code and it worked as expected on my dev site. Might want to double check your code.

    I did exactly that, but it didn’t help, unfortunately…

    You’re probably missing something then. Cannot help without seeing your code in action.

    The second (problematic) shortcode was put in “Ad Inserter” plugin

    Might be an issue with the Ad Inserter plugin then. Try adding your second shortcode to the end of your posts without that plugin and check again.

    Plugin Author Hector Cabrera

    (@hcabrera)

    And also: is it possible to add target=”_blank” rel=”noopener” to the first shortcode, and “target=_self” to the second shortcode?

    Of course, WordPress Popular Posts gives you full control of the HTML output. Might want to see the Wiki to learn how to change it to whatever you need. This might be a good place to start: How can I use my own HTML markup with WordPress Popular Posts?

    Thread Starter George

    (@giorgos93)

    @hcabrera

    Might be an issue with the Ad Inserter plugin then.

    I put the same code in content-single.php (wpapped in php tag), but it broke my styles. However, I tried another version of code made by Grok, and for some reason, it worked (both in content-single.php and Ad Inserter plugin):

    <?php
    if (function_exists('wpp_get_mostpopular')) {
        echo '<div class="wpp-custom-heading">ЧИТАЙТЕ ТАКЖЕ:</div>';
        wpp_get_mostpopular(array(
            'range' => 'last30days',
            'limit' => 6,
            'post_type' => 'post',
            'thumbnail_width' => 185,
            'thumbnail_height' => 140,
            'wpp_start' => '<div class="wpp-custom-cards">',
            'post_html' => '<div class="wpp-custom-card"><div class="wpp-custom-thumb">{thumb}</div><div class="wpp-custom-title"><a class="wpp-custom-title-link" href="{url}">{text_title}</a></div></div>',
            'wpp_end' => '</div>'
        ));
    }
    ?>

    Is this approach okay? And this version of code is okay, or should I change something?

    No, I don’t think that’s the case. I actually tested this code and it worked as expected on my dev site. Might want to double check your code.

    I did put it in functions.php, and now it did help for [wpp] at the end of each post (after changing code), but it doesn’t apply for [wpp] shortcode in sidebar (previous version of code worked for both shortcodes).

    • This reply was modified 6 months, 3 weeks ago by George.
    Plugin Author Hector Cabrera

    (@hcabrera)

    Alright, let’s just start over 😛

    Delete all previous code snippets from your functions.php file (including the ones generated by Grok) and use this instead:

    /**
    * Appends the [wpp] shortcode to the end of the content.
    *
    * @param string $content
    * @return string $content
    */
    function wp2674_append_wpp_shortcode_after_content($content) {
    if ( is_single() && in_the_loop() && is_main_query() ) {
    global $post;

    /* Get current post category IDs */
    $post_category_IDs = wp_get_post_categories( $post->ID, array( 'fields' => 'id=>parent' ) );

    /* Find the first child category ID so we can use it to filter posts */
    $child_category_ID = array_find_key(
    $post_category_IDs,
    function($parent_ID) {
    return $parent_ID > 0;
    }
    );

    /* This post isn't linked to a child category, fallback to the first ID */
    if ( ! $child_category_ID ) {
    $child_category_ID = array_key_first($post_category_IDs);
    }

    /* Shortcode settings */
    $range = 'last30days';
    $limit = 6;
    $post_type = 'post';
    $thumbnail_width = 185;
    $thumbnail_height = 140;
    $wpp_start = '<div class="wpp-custom-cards">';
    $post_html = '<div class="wpp-custom-card"><div class="wpp-custom-thumb">{thumb}</div><div class="wpp-custom-title">{text_title}</div></div>';
    $wpp_end = '</div>';

    $wpp_shortcode = do_shortcode("[wpp range='" . $range . "' limit=" . $limit . " post_type='" . $post_type . "' exclude='" . $post->ID . "' cat='" . $child_category_ID . "' thumbnail_width=" . $thumbnail_width . " thumbnail_height=" . $thumbnail_height . " wpp_start='" . $wpp_start . "' post_html='" . $post_html . "' wpp_end='" . $wpp_end . "' ajaxify=0]");

    /* Append shortcode to the end of the content */
    $content .= $wpp_shortcode;
    }

    return $content;
    }
    add_filter('the_content', 'wp2674_append_wpp_shortcode_after_content', 99);

    If you’re using a caching plugin on your site remember to flush its page cache so changes are applied to your site immediately.

    Thread Starter George

    (@giorgos93)

    Delete all previous code snippets from your functions.php file (including the ones generated by Grok) and use this instead:

    Again, when I put it in functions.php, it creates 6 posts, and each of them has unclickable title. But in thumbnails all posts links are used.

Viewing 15 replies - 1 through 15 (of 19 total)

The topic ‘Can WPP plugin replace YARPP?’ is closed to new replies.