• Resolved riyazmuhammad

    (@riyazmuhammad)


    Hi,

    I am successfully able show a part of website on my blog. But the Html contains too many items. I just wanted to show only first 4 items.

    My generated html is look like below:

    <div data-qs="property-list">
        <div>
            <div data-qs="properties"></div>
            <div data-qs="properties">
                <div class="cardlist cardlist-property">
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                    <div class="cardlist_item"></div>
                </div>
            </div>
        </div>
    </div>
    <div data-qs="property-list-end">

    from the above html I would like to show only first 4 <div class="cardlist_item"></div> items.

    My shortcode is look like:
    [remote_get url="https://www.example.com" start='<div data-qs="property-list">' stop='<div data-qs="property-list-end">']

    Thanks in advance,

    • This topic was modified 6 years, 7 months ago by riyazmuhammad.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Eli

    (@scheeeli)

    You will have to get creative here. There isn’t a built-in method for retrieving the first four occurrences of something.

    You could probably use CSS to hide all but the first four, or wrap that results in a fixed-height div with a scrollbar, or use a preg_replace statement to strip off all the remaining items after the first 4, but it will be tricky and it will take some creative thinking and experimentation.

    Thread Starter riyazmuhammad

    (@riyazmuhammad)

    Hi Eli,
    Thank you for the quick reply.
    Currently I am using css to hide all but the first four. But it loads all the divs. I think I have to find how to achieve this with preg_replace statement.

    Thanks again

    Thread Starter riyazmuhammad

    (@riyazmuhammad)

    Hi Eli,

    Can you please show some example shortcode which is using preg_replace statement?
    I couldn’t find anything on the plugin documentation regarding this.
    Please help.
    Thanks,

    Thread Starter riyazmuhammad

    (@riyazmuhammad)

    Hi Eli,
    I have created a shortcode with replace statement.
    [remote_get url="https://www.example.com" start='<div data-qs="property-list">' stop='<div data-qs="property-list-end">' replace='(?<=<div data-qs="properties">).*?<div class="cardlist cardlist-property">.*?<div data-qs="property-list-end">' with='(?<=<div class="cardlist cardlist-property">).*?<div class="cardlist_item">.*?<\/div>\s<div class="cardlist_item">.*?<\/div>\s<div class="cardlist_item">.*?<\/div>\s']

    When I test the regular expression in the ‘replace’ and ‘with’ part it display what I need exactly.
    But in the short code it is not working. Does the regex will not work in the shortcode?

    Thanks,

    Plugin Author Eli

    (@scheeeli)

    Sorry I missed your reply until today. The “replace” parameter of the remote_get shortcode uses str_replace not preg_replace. To use preg_replace you need to wrap the whole remote_get shortcode in a preg_replace shortcode, like this:

    
    [preg_replace replace='/<div data-qs="properties">(.*?)<div class="cardlist cardlist-property">(.*?)<div data-qs="property-list-end">/is' with='<div data-qs="properties">\1<div class="cardlist cardlist-property">\2']
    [remote_get url="https://www.example.com" start='<div data-qs="property-list">' end='<div data-qs="property-list-end">']
    [/preg_replace]
    

    P.S. This plugin is for advanced uses who already understand RegEx and cURL, it is not a good place to start learning because there are complicated deficiencies to using these functions within a shortcode that are highly compounded be WordPress’s interpretation of HTML in used in the content. I mostly posted this reply to help other with the same type of issue because I can tell from your code that you need a better understanding of RegEx before you are ready to play with shortcode usage and fight the up-hill battle with the WordPress HTML content “auto-correct feature”.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Dispaly only first 4 items from list’ is closed to new replies.