Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / wp_bootstrap_grid_get_posts.php
Last active December 5, 2017 12:34
WordPress & Bootstrap | Grid layout for get_posts
<?php
$args = array(
'posts_per_page' => 40,
'post_type' => 'page'
);
$posts = get_posts( $args );
if ( $posts ) {
$count = count( $posts );
$counter = 1; // Start the counter at 1
$cols_per_row = 4; // How many columns do you want per row?
@morgyface
morgyface / get_post_type_name_from_term.php
Last active December 1, 2020 13:51
WordPress | Function | Get the post type the Taxonomy Term is related to
<?php
function get_post_type_name( $term_id ) {
// Here we use the term id to identify the name of the post type the related taxonomy is registered to
$term = get_term( $term_id );
$taxonomy = $term->taxonomy;
$tax_object = get_taxonomy( $taxonomy ); // Get the taxonomy object
$post_types = $tax_object->object_type; // Get the object type (as an array) the taxonomy is registered to
$post_type_object = get_post_type_object( $post_types[0] ); // Use the object type to get details on the object type
$post_type_name = $post_type_object->labels->name; // Get the name label of the the object
return $post_type_name;
@morgyface
morgyface / any_parents.php
Created December 19, 2017 12:21
WordPress | Function | Check to see if a post has any parents
<?php
// Function to check if post has any parents
function is_bastard() {
global $post;
$parent = $post->post_parent;
if( $parent == 0 ) {
return true;
} else {
return false;
}
@morgyface
morgyface / has_children.php
Last active December 19, 2017 12:34
WordPress | Function | Check to see if a post has any children
<?php
// True or false function to check for child pages
function has_children() {
global $post;
$children = get_pages(
array(
'child_of' => $post->ID
)
);
if( count( $children ) == 0 ) {
@morgyface
morgyface / parent_title.php
Created December 19, 2017 12:23
WordPress | Function | Get the title of a post's parent page
<?php
// Function to return parent page title
function parent_title() {
global $post;
$parent_id = wp_get_post_parent_id( $post->ID );
$parent_post = get_post( $parent_id );
$parent_post_title = $parent_post->post_title;
return $parent_post_title;
}
?>
@morgyface
morgyface / parent_template_name.php
Created December 19, 2017 12:27
WordPress | Function | Return the name of the template the posts parent uses
<?php
function parent_template() {
global $post;
$parent_id = wp_get_post_parent_id( $post->ID );
$parent_template_name = get_page_template_slug( $parent_id );
return $parent_template_name;
}
?>
@morgyface
morgyface / child_pages_only.php
Created December 19, 2017 12:33
WordPress | Only return child pages NOT grandchildren
<?php
$childpages = get_pages(
array(
'child_of' => $post->ID,
'parent' => $post->ID
)
);
foreach( $childpages as $page ) {
$page_title = $page->post_title;
$page_link = get_page_link( $page->ID );
@morgyface
morgyface / wp_meta_description_function.php
Last active March 10, 2020 18:25
WordPress | Function | Use post/page excerpt to generate meta description for SEO
<?php
// Generating a meta description using standard WP components
function meta_description( $char_limit ) {
$tagline = get_bloginfo ( 'description' );
$post_object = get_post();
$excerpt = $post_object->post_excerpt; // Get the raw excerpt, warts (tags) and all.
$content = $post_object->post_content; // Get the raw content.
if ( !empty( $excerpt ) ) { // If there is an excerpt lets use it to generate a meta description
$excerpt_stripped = strip_tags( $excerpt ); // Remove any tags using the PHP function strip_tags.
$excerpt_length = strlen( $excerpt_stripped ); // Now lets count the characters
@morgyface
morgyface / trim_the_excerpt.php
Last active January 30, 2018 16:18
WordPress | Function | Trim the excerpt
<?php
function trim_the_excerpt( $char_limit, $ellipsis = true ) {
$excerpt = get_the_excerpt();
$excerpt_stripped = strip_tags( $excerpt ); // Remove any tags using the strip_tags function.
$excerpt_length = strlen( $excerpt_stripped ); // Now lets count the characters so we can compare
if ( $excerpt_length > $char_limit ) { // Now work out if we need to trim the character length.
$offset = $char_limit - $excerpt_length; // This gives us a negative value.
$position = strrpos( $excerpt_stripped, ' ', $offset ); // This starts looking for a space backwards from the offset
$trimmed_excerpt = substr( $excerpt_stripped, 0, $position ); // Trim up until the point of the last space.
$last_character = substr( $trimmed_excerpt, -1 ); // Identify the last character of the newly trimmed excerpt
@morgyface
morgyface / repeater.php
Created January 31, 2018 09:51
Wordpress | ACF | Basic repeater setup
<?php
if( have_rows('repeater_field_name') ):
echo '<ul>';
while( have_rows('repeater_field_name') ): the_row();
$name = get_sub_field('name');
echo '<li>';
echo '<h3>' . $name . '</h3>';
echo '</li>';
endwhile;
echo '</ul>';