Last active
July 29, 2016 02:01
-
-
Save rgadon107/091bde22f6ece38dc20e0dcd50d11a90 to your computer and use it in GitHub Desktop.
Change the 'Read More...' links in WP core & Genesis; Post columns grid patterns to allow 2 - 6 grid columns.
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 | |
namespace albemishpc\post; | |
/** | |
* Post structure handling. | |
* | |
* @package Utility Pro\post | |
* | |
* @since 1.0.0 | |
* | |
* @author Robert A. Gadon | |
* | |
* @link http://spiralwebdb.com | |
* | |
* @license GNU General Public License 2.0+ | |
*/ | |
add_filter( 'the_content_more_link', __NAMESPACE__ . '\change_the_read_more_link' ); | |
add_filter( 'get_the_content_more_link', __NAMESPACE__ . '\change_the_read_more_link' ); | |
/** | |
* Change the default 'Read More...' link HTML markup and content. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $html | |
* | |
* @return string | |
*/ | |
function change_the_read_more_link( $html ) { | |
$html = change_read_more_text( $html, __( 'Continue reading', 'utility-pro' ) ); | |
if ( doing_filter( 'get_the_content_more_link' ) ) { | |
$html = strip_off_read_more_opening_dots( $html ); | |
return '</p><p>' . $html; | |
} | |
return sprintf( '<p>%s</p>', $html ); | |
} | |
/** | |
* Strips off the read more link's opening dot pattern. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $html | |
* @param string $dots Dots pattern to strip off | |
* | |
* @return string | |
*/ | |
function strip_off_read_more_opening_dots( $html, $dots = '… ' ) { | |
return substr( $html, strlen( $dots ) ); | |
} | |
/** | |
* Replace the read more text from the Genesis default text of '[Read more...]' to | |
* the new specified replacement text. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $html Read more link HTML | |
* @param string $replacement_text Replacement text | |
* | |
* @return string | |
*/ | |
function change_read_more_text( $html, $replacement_text ) { | |
$text_to_replace = __( '[Read more...]', 'utility-pro' ); | |
return str_replace( $text_to_replace, $replacement_text, $html ); | |
} | |
add_filter( 'post_class', __NAMESPACE__ . '\add_to_post_classes_for_grid_pattern' ); | |
/** | |
* Add Attributes to the Post class | |
* | |
* @since 1.0.0 | |
* | |
* @param array $classes An Array of post classes. | |
* @return array $classes An Array of post classes. | |
*/ | |
function add_to_post_classes_for_grid_pattern( array $classes ) { | |
if ( ! is_array( $classes ) ) { | |
return $classes; | |
} | |
if ( ! is_home() ) { | |
return $classes; | |
} | |
// Default $number_of_columns = 2. | |
// Pass an integer from 2 - 6 along with $classes to specify number_of_columns. | |
return get_classes_for_grid_pattern( $classes, 2 ); | |
} | |
/** | |
* Based on the number of columns requested, get the styling classes for the grid pattern. | |
* | |
* @since 1.0.0 | |
* | |
* @param array $classes Post classes. | |
* @param int $number_of_classes Number of columns to set for this grid pattern. | |
* | |
* @return array | |
*/ | |
function get_classes_for_grid_pattern( array $classes, $number_of_columns = 2 ) { | |
if ( $number_of_columns < 2 || $number_of_columns > 6 ) { | |
return $classes; | |
} | |
// Call the global instance of the WP_Query object to interact with the $current_post method below. | |
global $wp_query; | |
// The index position with the array equals $number_of_columns. | |
$column_classes = array( | |
'', // index 0 | |
'', // index 1 | |
'one-half', // index 2 = represents 2 columns | |
'one-third', // index 3 | |
'one-fourth', // index 4 | |
'one-fifth', // index 5 | |
'one-sixth', // index 6 | |
); | |
// Copy the column class out of the array above, and add it to the end of the $classes array. | |
if ( is_post_of_post_type() ) { | |
$classes[] = $column_classes[ $number_of_columns ]; | |
} | |
// Add the 'first' styling class to $classes[] when the modulus remainder == 0. | |
if ( $wp_query->current_post % $number_of_columns == 0 ) { | |
$classes[] = 'first'; | |
} | |
return $classes; | |
} | |
/** | |
* Checks if the current (or specified) post is of the specified post type. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $post_type | |
* @param int|WP_Post|null $post_or_post_id Post ID or post object. When `null`, | |
* WordPress uses global $post. | |
* | |
* @see get_post_type() Retrieve the post type of the current post or of a given post. | |
* | |
* @return bool | |
*/ | |
function is_post_of_post_type( $post_type = 'post', $post_or_post_id = null ) { | |
return get_post_type( $post_or_post_id ) == $post_type; | |
} |
Add code to change the Read More...
links in WordPress core and Genesis.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Per Tonya Mork (@hellofromtonya), make the following changes:
global $post
variable in theget_classes_for_grid_pattern()
callback. The global variable can be overwritten, and consequently affect the entire behavior of a site in unintended ways.get_post_type()
. By default, this function returns the global$post
object when the parameter is set to$post = null
.is_post_of_post_type()
that accepts the type of post type you wish to check, and then pass in theget_post_type()
function, and return the callback as a conditional that checks for a boolean value. Use the helper function inget_classes_for_grid_pattern()
with a conditional statement to target the use of the desired post_type.