Last active
December 26, 2015 13:29
-
-
Save SdVGits/7158795 to your computer and use it in GitHub Desktop.
Erweitert das "Toolbox-Modul für die Ausgabe der Twitter Cards mit wpSEO Metadaten in WordPress-Artikelseiten" von @sergejmueller. + eigene Twitter-Cards für die Wordpress Post-Formats "gallery" und "image". + Für Posts ohne Post-Thumbnail wird das Erste für den Post hochgeladene (oder verknüpfte) Bild für die Twitter-Card ausgewählt. + Für Post…
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 | |
/* | |
Module Name: Twitter Cards | |
Module URI: http://helpdesk.wpseo.de/manual/twitter-cards/ | |
Description: Ausgabe der Twitter Cards mit wpSEO Metadaten. [Frontend] | |
Author: Sergej Müller | |
Author URI: http://wpcoder.de | |
*/ | |
/* Sicherheitsabfrage */ | |
if ( !class_exists('Toolbox') ) { | |
die(); | |
} | |
/* Ab hier kann's los gehen */ | |
function wpseo_twitter_cards() { | |
if ( is_feed() OR is_trackback() OR ! ( is_singular() OR is_front_page() ) ) { | |
return; | |
} | |
//Photo-Card beim Post-Format "Image" | |
if ( has_post_format( 'image' , get_the_ID() ) && ! is_front_page()) { ?> | |
<meta name="twitter:card" value="photo" /> | |
<?php } | |
//Gallery-Card beim Post-Format "Gallery" | |
elseif ( has_post_format( 'gallery' , get_the_ID() ) && ! is_front_page()) { ?> | |
<meta name="twitter:card" value="gallery" /> | |
<?php | |
// Die ersten 4 Bilder (Anhänge) des Posts für die Twitter-Gallery-Card ausgeben. | |
$args = array( 'post_type' => 'attachment', 'numberposts' => 4, 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => get_the_ID() ); | |
$attachments = get_posts($args); | |
$imagecounter = 0; | |
if ($attachments) { | |
foreach ( $attachments as $attachment ) { | |
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ); ?> | |
<meta name="twitter:image<?php echo $imagecounter++?>" value="<?php echo $image_attributes[0]; ?>" /> | |
<?php } | |
} | |
} | |
// Für Posts mit Postthumbnail die Twitter-Card mit großem Bild auswählen. | |
elseif (is_singular() && has_post_thumbnail( get_the_ID() ) && ! has_post_format( 'image' , get_the_ID() ) && ! has_post_format( 'gallery' , get_the_ID() )) { ?> | |
<meta name="twitter:card" value="summary_large_image" /> | |
<?php } | |
// Für alle anderen Post-Formats die Twitter-Card "Summary" festlegen. | |
else { ?> | |
<meta name="twitter:card" value="summary" /> | |
<?php } ?> | |
<meta name="twitter:site" value="@website-account"/> | |
<meta name="twitter:creator" value="@author-account" /> | |
<meta name="twitter:url" value="<?php the_permalink() ?>" /> | |
<meta name="twitter:title" value="<?php do_action('wpseo_the_title') ?>" /> | |
<meta name="twitter:description" value="<?php do_action('wpseo_the_desc') ?>" /> | |
<?php | |
if ( is_singular() && has_post_thumbnail(get_the_ID()) && ! has_post_format( 'gallery' , get_the_ID() ) ) { | |
$post_thumbs = wp_get_attachment_image_src( | |
get_post_thumbnail_id( get_the_ID() ), | |
'full' | |
); | |
if ( ! empty($post_thumbs[0]) ) { ?> | |
<meta name="twitter:image" value="<?php echo esc_url($post_thumbs[0]) ?>" /> | |
<?php } | |
} | |
if ( is_front_page() || ! has_post_thumbnail( get_the_ID() ) ) { | |
$args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => get_the_ID() ); | |
$attachments = get_posts($args); | |
if (! is_front_page() && $attachments ) { | |
foreach ( $attachments as $attachment ) { | |
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ); ?> | |
<meta name="twitter:image" value="<?php echo $image_attributes[0]; ?>" /> | |
<?php } | |
} | |
else {?> | |
<meta name="twitter:image" value="FALLBACK URL" /> | |
<?php | |
} | |
} | |
} | |
add_action('wp_head', 'wpseo_twitter_cards', 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment