Support » Plugin: Link Library » Custom Table

  • Resolved Fred Atkinson

    (@fatkinson)


    Yannick,

    Is there any way to configure a Library Configuration so that only a limited number of randomly chosen links [from a particular category] are displayed?

    The idea is to put a shortcode on the main page so that a few random links are displayed as part of the content. They should be sorted by the ‘sortcode’ custom field so they appear in the correct order.

    Let’s say a 3×5 table is desired. It should then [very randomly] select fifteen links from the category, sort them by the ‘sortcode’ value, and then display the Name linked to the URL of each record in the 3×5 table. And each time the page is reloaded, a different set of fifteen links should appear.

    Is this currently doable?

    Regards,

    Fred

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

Viewing 15 replies - 1 through 15 (of 26 total)
  • Plugin Author Yannick Lefebvre

    (@jackdewey)

    Yes, mostly, except for the past that says to sort by the sortcode value. If the links are randomly chosen, they will be not be displayed based on their sortcode value.

    Thread Starter Fred Atkinson

    (@fatkinson)

    It loses something without that capability.
    You have to be an amateur radio operator to understand why it’s so important.
    I will see if I can figure out how to do that table.

    Thread Starter Fred Atkinson

    (@fatkinson)

    Well, I pulled it off. Take a look!: https://www.wb4aej.com/test/
    I will be putting that short code on my main page shortly.

    Thread Starter Fred Atkinson

    (@fatkinson)

    The only issue is that it always uses the first fifteen links.
    How do I make it select them randomly?

    Plugin Author Yannick Lefebvre

    (@jackdewey)

    As I said, if you make it take random links, they will also be ordered randomly.

    Thread Starter Fred Atkinson

    (@fatkinson)

    I understood that perfectly.
    But it is not displaying random links.
    It is displaying the first fifteen every time (not dynamic content).

    Plugin Author Yannick Lefebvre

    (@jackdewey)

    Right. If you leave the order method to anything other than random, then it will always take the first X items based on the sort method selected. If you want dynamic content, you need to set the sort method to random, which will mean that the random items will not be ordered.

    I will see what else I could do, but I can’t promise anything.

    Thread Starter Fred Atkinson

    (@fatkinson)

    Sorry, I didn’t see that Random option.
    It’s looking good now!

    Plugin Author Yannick Lefebvre

    (@jackdewey)

    So… do you need me to do anything on this or is this resolved?

    Thread Starter Fred Atkinson

    (@fatkinson)

    For the sake of discussion, let me explain the sort method I use.
    Amateur radio calls consist of a prefix (1 or 2 letters or one letter and a numeral; the prefix identifies the nation in which the station is licensed), the call district (1 numeral) of the nation the station resides in, and the suffix (1 to 3 letters) that uniquely identifies the station.
    In an annual publication known as Callbook, the call signs were listed in this order: First by call district, then by suffix, then by prefix.
    When I created the Ham Domain database, I wrote a script to sort them in this order.
    First, I did it with CGI. Later, I did it with PHP.
    I could only accomplish it using a hidden value which I called the ‘sortcode’. It was never displayed. But it was instrumental in getting it in the right order.
    When an Amateur Radio Operator looks up a call, it is easiest if they are listed in this order.
    I am not a great programmer. But I can usually accomplish it if I give it the amount of time needed. That may be a good deal of time.
    I wrote a blurb explaining the dynamics of my sorting method. It was on my old Web site (before WordPress).
    I just republished it on the same URL as before. You can now read it at: https://www.wb4aej.com/hamsort .

    Plugin Author Yannick Lefebvre

    (@jackdewey)

    Thanks for the link, but I am not sure if what you have now works you for or if you’d still need to be able to get 15 random links and be able to sort them by custom text field #1? Or if what’s in place now is ok for you.

    Thread Starter Fred Atkinson

    (@fatkinson)

    It works, no problem.
    I would still like to sort them in this way if it is at all possible.
    But it works.

    Plugin Author Yannick Lefebvre

    (@jackdewey)

    As discussed via e-mail, I have added two new fields at the bottom of the Library Configurations Advanced tab to allow users to set a custom WP Query argument and value for that argument. Once set, the following code in your theme’s functions.php file will sort the randomly selected items:

    // Filter to sort the random posts displayed
    add_filter( 'the_posts', function ( $posts, $q )
    {
        // First we need remove our filter
        remove_filter( current_filter(), __FUNCTION__ );
    
        // Check if our custom parameter is set and is true, if not, bail early
        if ( empty( $q->get( 'randomlinknamesort' ) ) ) {
            return $posts; 
        }             
    
        $top_10_items = array_splice( $posts, 0, intval( $q->get( 'randomlinknamesort' ) ) );
    
        foreach ( $top_10_items as $key => $item ) {
            $top_10_items[$key]->link_custom_text = get_post_meta( $item->ID, 'link_custom_text_1', true );
        }
    
        usort( $top_10_items, function ( $item1, $item2 ) {
            return $item1->link_custom_text <=> $item2->link_custom_text;
        });
        
        // Make sure we have a value for $major_array, if not, bail
        if ( empty( $top_10_items ) ) {
            return $posts;
        }        
    
        return array_values( $top_10_items );
    }, 10, 2 );
    Thread Starter Fred Atkinson

    (@fatkinson)

    Ok. I will have to remember to put that in the PHP file if I rebuild the site or if I change the theme.
    I have copied those lines into my iPad so I will have them if/when I need them.
    Thanks, Yannick.

    Regards,

    Fred

    Thread Starter Fred Atkinson

    (@fatkinson)

    Yannick,

    I am trying to find that ‘functions.php’ file in link-library.

    I checked every subdirectory but didn’t find it.

    I want to upgrade to the latest version of Link Library. But I don’t want to lose the sort routine when I do (I believe you said I would have to insert it each time the plugin was updated).

    Regards,

    Fred

Viewing 15 replies - 1 through 15 (of 26 total)
  • You must be logged in to reply to this topic.