Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reusable Snippets Post
<?php
function _s_remove_archive_title_prefix( $title ) {
// Get the single category title with no prefix.
$single_cat_title = single_term_title( '', false );
if ( is_category() || is_tag() || is_tax() ) {
return esc_html( $single_cat_title );
}
return $title;
}
add_filter( 'get_the_archive_title', '_s_remove_archive_title_prefix' );
<?php
function _s_add_og_tags() {
// Bail if Yoast is installed, since it will handle things.
if ( class_exists( 'WPSEO_Options' ) ) {
return '';
}
// Set a post global on single posts. This avoids grabbing content from the first post on an archive page.
if ( is_singular() ) {
global $post;
}
// Get the post content.
$post_content = ! empty( $post ) ? $post->post_content : '';
// Strip all tags from the post content we just grabbed.
$default_content = ( $post_content ) ? wp_strip_all_tags( strip_shortcodes( $post_content ) ) : $post_content;
// Set our default title.
$default_title = get_bloginfo( 'name' );
// Set our default URL.
$default_url = get_permalink();
// Set our base description.
$default_base_description = ( get_bloginfo( 'description' ) ) ? get_bloginfo( 'description' ) : esc_html__( 'Visit our website to learn more.', '_s' );
// Set the card type.
$default_type = 'article';
// Get our custom logo URL. We'll use this on archives and when no featured image is found.
$logo_id = get_theme_mod( 'custom_logo' );
$logo_image = ( $logo_id ) ? wp_get_attachment_image_src( $logo_id, 'full' ) : '';
$logo_url = ( $logo_id ) ? $logo_image[0] : '';
// Set our final defaults.
$card_title = $default_title;
$card_description = $default_base_description;
$card_long_description = $default_base_description;
$card_url = $default_url;
$card_image = $logo_url;
$card_type = $default_type;
// Let's start overriding!
// All singles.
if ( is_singular() ) {
if ( has_post_thumbnail() ) {
$card_image = get_the_post_thumbnail_url();
}
}
// Single posts/pages that aren't the front page.
if ( is_singular() && ! is_front_page() ) {
$card_title = get_the_title() . ' - ' . $default_title;
$card_description = ( $default_content ) ? wp_trim_words( $default_content, 53, '...' ) : $default_base_description;
$card_long_description = ( $default_content ) ? wp_trim_words( $default_content, 140, '...' ) : $default_base_description;
}
// Categories, Tags, and Custom Taxonomies.
if ( is_category() || is_tag() || is_tax() ) {
$term_name = single_term_title( '', false );
$card_title = $term_name . ' - ' . $default_title;
$specify = ( is_category() ) ? esc_html__( 'categorized in', '_s' ) : esc_html__( 'tagged with', '_s' );
$queried_object = get_queried_object();
$card_url = get_term_link( $queried_object );
$card_type = 'website';
// Translators: get the term name.
$card_long_description = $card_description = sprintf( esc_html__( 'Posts %1$s %2$s.', '_s' ), $specify, $term_name );
}
// Search results.
if ( is_search() ) {
$search_term = get_search_query();
$card_title = $search_term . ' - ' . $default_title;
$card_url = get_search_link( $search_term );
$card_type = 'website';
// Translators: get the search term.
$card_long_description = $card_description = sprintf( esc_html__( 'Search results for %s.', '_s' ), $search_term );
}
if ( is_home() ) {
$posts_page = get_option( 'page_for_posts' );
$card_title = get_the_title( $posts_page ) . ' - ' . $default_title;
$card_url = get_permalink( $posts_page );
$card_type = 'website';
}
// Front page.
if ( is_front_page() ) {
$front_page = get_option( 'page_on_front' );
$card_title = ( $front_page ) ? get_the_title( $front_page ) . ' - ' . $default_title : $default_title;
$card_url = get_home_url();
$card_type = 'website';
}
// Post type archives.
if ( is_post_type_archive() ) {
$post_type_name = get_post_type();
$card_title = $post_type_name . ' - ' . $default_title;
$card_url = get_post_type_archive_link( $post_type_name );
$card_type = 'website';
}
// Media page.
if ( is_attachment() ) {
$attachment_id = get_the_ID();
$card_image = ( wp_attachment_is_image( $attachment_id ) ) ? wp_get_attachment_image_url( $attachment_id, 'full' ) : $card_image;
}
?>
<meta property="og:title" content="<?php echo esc_attr( $card_title ); ?>" />
<meta property="og:description" content="<?php echo esc_attr( $card_description ); ?>" />
<meta property="og:url" content="<?php echo esc_url( $card_url ); ?>" />
<meta property="og:image" content="<?php echo esc_url( $card_image ); ?>" />
<meta property="og:site_name" content="<?php echo esc_attr( $default_title ); ?>" />
<meta property="og:type" content="<?php echo esc_attr( $card_type ); ?>" />
<meta name="description" content="<?php echo esc_attr( $card_long_description ); ?>" />
<?php
}
add_action( 'wp_head', '_s_add_og_tags' );
<?php
function _s_entry_footer() {
// Hide category and tag text for pages.
if ( 'post' === get_post_type() ) {
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( esc_html__( ', ', '_s' ) );
if ( $categories_list && _s_categorized_blog() ) {
/* translators: the post category */
printf( '<span class="cat-links">' . esc_html__( 'Posted in %1$s', '_s' ) . '</span>', $categories_list ); // WPCS: XSS OK.
}
/* translators: used between list items, there is a space after the comma */
$tags_list = get_the_tag_list( '', esc_html__( ', ', '_s' ) );
if ( $tags_list ) {
/* translators: the post tags */
printf( '<span class="tags-links">' . esc_html__( 'Tagged %1$s', '_s' ) . '</span>', $tags_list ); // WPCS: XSS OK.
}
}
if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
echo '<span class="comments-link">';
comments_popup_link( esc_html__( 'Leave a comment', '_s' ), esc_html__( '1 Comment', '_s' ), esc_html__( '% Comments', '_s' ) );
echo '</span>';
}
edit_post_link(
sprintf(
/* translators: %s: Name of current post */
esc_html__( 'Edit %s', '_s' ),
the_title( '<span class="screen-reader-text">"', '"</span>', false )
),
'<span class="edit-link">',
'</span>'
);
}
<?php
function _s_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf(
$time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
/* translators: the date the post was published */
esc_html_x( 'Posted on %s', 'post date', '_s' ),
'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);
$byline = sprintf(
/* translators: the post author */
esc_html_x( 'by %s', 'post author', '_s' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
<?php
function _s_setup() {
// Register navigation menus.
register_nav_menus( array(
'social' => esc_html__( 'Social Networks Menu', '_s' ),
) );
}
add_action( 'after_setup_theme', '_s_setup' );
$social-sites: facebook twitter linkedin instagram youtube;
.menu-name {
a {
// Set the background image for each social network.
@each $network in $social-sites {
&[href*='#{$network}'] {
background: url('assets/images/svg-icons/#{$network}.svg') 0 0 no-repeat;
background-size: rem(32);
@include media($tablet-portrait) {
background-size: rem(16);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.