Skip to content

Instantly share code, notes, and snippets.

@rgadon107
Last active July 29, 2016 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgadon107/091bde22f6ece38dc20e0dcd50d11a90 to your computer and use it in GitHub Desktop.
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.
<?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 = '&#x02026; ' ) {
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;
}
@rgadon107
Copy link
Author

rgadon107 commented Jul 21, 2016

Initial commit. Two functions generated; add_to_post_classes_for_grid_pattern(), and get_classes_for_grid_pattern(). The former displays on posts pages, and the latter calls the CSS classes depending on the number of columns defined. Based on the code presented during the 'KnowTheCodeShow' podcast, Episode 3 (https://www.youtube.com/watch?v=Ge17RwBrGDY).

Note: When the integer 2 is includes as a parameter on line 24 above, the code causes the custom post type 'Practice Areas' on the front page to move up next to the 'Promotion' post type on the same page (title area: Professional Profile).

@rgadon107
Copy link
Author

rgadon107 commented Jul 22, 2016

  1. Called global $post object within get_classes_for_grid_pattern() callback.
  2. Set conditional to limit addition of column classes to 'post' post_type.

Custom columns now display only on 'blog' page, and does not affect display of custom post types.

@rgadon107
Copy link
Author

rgadon107 commented Jul 24, 2016

Per Tonya Mork (@hellofromtonya), make the following changes:

  1. Avoid use of the global $post variable in the get_classes_for_grid_pattern() callback. The global variable can be overwritten, and consequently affect the entire behavior of a site in unintended ways.
  2. As an alternative, use the WordPress API by using the function get_post_type(). By default, this function returns the global $post object when the parameter is set to $post = null.
  3. Create a helper function is_post_of_post_type() that accepts the type of post type you wish to check, and then pass in the get_post_type() function, and return the callback as a conditional that checks for a boolean value. Use the helper function in get_classes_for_grid_pattern() with a conditional statement to target the use of the desired post_type.

@rgadon107
Copy link
Author

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