Last active
May 28, 2019 02:44
-
-
Save sc0ttkclark/a4e6460db78ad2464148e6123322819f to your computer and use it in GitHub Desktop.
Custom `[pods]` shortcode handling for total text.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Ensure total_found() gets calculated right away, because in the [pods] shortcode | |
* it does other queries after the initial find(). | |
*/ | |
add_filter( 'pods_data_auto_calculate_total_found', '__return_true' ); | |
/** | |
* Add custom shortcode handling for total text. | |
* | |
* [pods name="mypod" limit="15" total_text="Total"] | |
* ..the template content.. | |
* Total: 15 | |
* | |
* [pods name="mypod" limit="15" total_position="before"] | |
* Total: 15 | |
* ..the template content.. | |
* | |
* [pods name="mypod" limit="15" total_text="%s record(s)"] | |
* ..the template content.. | |
* 15 record(s) | |
* | |
* [pods name="mypod" limit="15" total_text="%s record(s)" total_singular_text="%s record"] | |
* ..the template content.. | |
* 1 record | |
* | |
* [pods name="mypod" limit="15" total_found_text="Total found"] | |
* ..the template content.. | |
* Total: 15 | |
* | |
* [pods name="mypod" limit="15" total_found_text="%s record(s) found"] | |
* ..the template content.. | |
* 15 record(s) found | |
* | |
* [pods name="mypod" limit="15" total_found_text="%s records found" total_found_singular_text="%s record found"] | |
* ..the template content.. | |
* 1 record found | |
* | |
* @param string $return Shortcode output to return. | |
* @param array $tags Shortcode attributes. | |
* @param Pods $pod Pods object. | |
* | |
* @return string Shortcoude output. | |
*/ | |
function custom_pods_shortcode_total_text( $return, $tags, $pod ) { | |
// Only handle for non-singular cases. | |
if ( empty( $pod->id ) ) { | |
$text = null; | |
$total = null; | |
$position = 'after'; | |
if ( ! empty( $tags['total_position'] ) ) { | |
$position = $tags['total_position']; | |
} | |
if ( ! empty( $tags['total_text'] ) ) { | |
// Total text. | |
$text = $tags['total_text']; | |
$total = $pod->total(); | |
if ( ! empty( $tags['total_singular_text'] ) && 1 === $total ) { | |
$text = $tags['total_singular_text']; | |
} | |
} elseif ( ! empty( $tags['total_found_text'] ) ) { | |
// Total found text. | |
$text = $tags['total_found_text']; | |
$total = $pod->total_found(); | |
if ( ! empty( $tags['total_found_singular_text'] ) && 1 === $total ) { | |
$text = $tags['total_found_singular_text']; | |
} | |
} | |
if ( $text ) { | |
$total = number_format_i18n( $total ); | |
// Total text already has a placeholder for the total number. | |
if ( false !== strpos( $text, '%s' ) ) { | |
$text = sprintf( $text, $total ); | |
} else { | |
$text = sprintf( '%1$s: %2$s', $text, $total ); | |
} | |
$text = sprintf( "<p class=\"pods-total-text\">%s</p>\n", $text ); | |
if ( 'before' === $position ) { | |
$return = $text . $return; | |
} else { | |
$return .= $text; | |
} | |
} | |
} | |
return $return; | |
} | |
add_filter( 'pods_shortcode_output', 'custom_pods_shortcode_total_text', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment