WordPress.org

Ready to get started?Download WordPress

Forums

Mini Loops
[resolved] How about inserting PHP code into Item format ? (12 posts)

  1. sugardaddy
    Member
    Posted 1 year ago #

    For anything else, I wouldn't have to use PHP inside this plugin, but... I need to hardcode gettext expressions. And I'm a bit lazy : I don't want to duplicate all my widgets in all langages I need (and use conditionnal tags).
    Is there a way to achieve that ?

    Thanks for your help !

    http://wordpress.org/extend/plugins/mini-loops/

  2. sugardaddy
    Member
    Posted 1 year ago #

    Well... never any answer for this plugin.
    You should open it on a repository to have it upgraded by other people if you don't have time...

  3. sugardaddy
    Member
    Posted 1 year ago #

    Ok I did otherwise, but it's no more with GUI... the main interest of mini loops.
    To achieve a translation of a mini loop and the strings inside items format, I used WPML, Executable PHP widget and the shortcode of mini loop.

    First of all, you should try your loop with mini loop. In this way, you will be aware of what option to add to the shortcode and be sure the loop works.

    Use the executable PHP widget to create a widget in which you can write some PHP. You need it to invoke the WPML conditional tags.

    And there's the result :

    <?php
    if (ICL_LANGUAGE_CODE == 'fr') {
        echo '<div>'; // this is what I usually put in the "before items" section of mini loop
        echo do_shortcode('[miniloop categories=46 number_posts=1 shuffle_order=1 order_by="random"]{image from=attached width=70 height=auto cache=clear}<h4><a href="{url}" title="En savoir plus sur le projet de {title}">{title}</a></h4><p>{excerpt custom=1}</p>[/miniloop]');
        echo '</div><div class="more"><a href="/clients" title="Tous nos clients"><span>Tous nos clients</span></a></div>'; // this is what I usually put in the "after items" section of mini loop
    }
    elseif (ICL_LANGUAGE_CODE=='en') {
       echo '<div class="widget-content client">';
        echo do_shortcode('[miniloop categories=46 number_posts=1 shuffle_order=1 order_by="random"]{image from=attached width=70 height=auto cache=clear}<h4><a href="{url}" title="Read more about the project of {title}">{title}</a></h4><p>{excerpt custom=1}</p>[/miniloop]');
        echo '</div><div class="more"><a href="/clients-2" title="More clients"><span>More clients</span></a></div>';
    }
    else {}
    ?>

    And there's a list of the arguments supported by the shortcode :

    $args = array(
            'title' => __( 'Recent Posts', 'mini-loops' ),
            'hide_title' => 0,
            'title_url' => '',
            'number_posts' => 3,
            'post_offset' => 0,
            'post_type' => 'post',
            'post_status' => 'publish',
            'order_by' => 'date',
            'order' => 'DESC',
            'reverse_order' => 0,
            'shuffle_order' => 0,
            'ignore_sticky' => 1,
            'only_sticky' => 0,
            'exclude_current' => 1,
            'categories' => '',
            'tags' => '',
            'tax' => '',
            'custom_fields' => '',
            'exclude' => '',
            'before_items' => '<ul>',
            'item_format' => '<li><a href="[url]">[title]</a><br />[excerpt]</li>',
            'after_items' => '</ul>',
            );

    Of course, I could have put the code before and after inside the shortcode... but It's outside for readability and to bulletproof the shortcode with less arguments.

    Glad if it could help. But the ability to have gettext expressions inside the items format (before and after too) would be a great feature...

  4. Kailey (trepmal)
    Volunteer Mod & Reviewer
    Plugin Author

    Posted 1 year ago #

    You might be able to use the miniloops_item_format filter. For example, in the item format put a placeholder string, then use that filter to replace the string with whatever you need:

    add_filter( 'miniloops_item_format', function( $format ) {
    	$string1 = __( 'Some text', 'text-domain');
    	$format = str_replace( '###', $string1, $format );
    	return $format;
    });
  5. sugardaddy
    Member
    Posted 1 year ago #

    That's a bit easier and clean... thanks a lot !!

  6. sugardaddy
    Member
    Posted 1 year ago #

    And... just one more question (as I'm not a developer) : how to do that for many strings ? Should I make a function for each string ?

  7. sugardaddy
    Member
    Posted 1 year ago #

    Sorry for that PHP question... I found by myself.
    But as I want to include placeholders for translations into before and after item, I have to create filters for that placements.

  8. Kailey (trepmal)
    Volunteer Mod & Reviewer
    Plugin Author

    Posted 1 year ago #

    There are similar hooks for both the before and after fields. For those, use either miniloops_before_items_format or miniloops_after_items_format in place of miniloops_item_format

  9. sugardaddy
    Member
    Posted 1 year ago #

    Yes I found it before your answer, and it works like a charm !!

  10. sugardaddy
    Member
    Posted 1 year ago #

    One more thing to close this topic : actually, I'm working with WPML and, as an advice, you should add an apply_filters to your widgets'titles.
    That's what I did, but I have to do it on each update...

  11. Kailey (trepmal)
    Volunteer Mod & Reviewer
    Plugin Author

    Posted 1 year ago #

    Adding thewidget_title filter in my list for the next update.

  12. sugardaddy
    Member
    Posted 1 year ago #

    Thanks a lot !!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic