我使用此代码在帖子顶部显示社交媒体共享链接。目前,它们在我的页面和帖子上显示良好。有没有办法让它也显示在我的分类页面上?另一个可行的选择是让代码显示在H1和内容下方的任何地方,我认为没有任何页面不需要这些按钮,所以这可能是更简单的解决方案。
任何指导都将不胜感激☺️
function crunchify_social_sharing_buttons($content) {
global $post;
if(is_singular() || is_home()){
// Get current page URL
$crunchifyURL = urlencode(get_permalink());
// Get current page title
$crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, \'UTF-8\')), ENT_COMPAT, \'UTF-8\');
// $crunchifyTitle = str_replace( \' \', \'%20\', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full\' );
// Construct sharing URL without using any script
$twitterURL = \'https://twitter.com/intent/tweet?text=\'.$crunchifyTitle.\'&url=\'.$crunchifyURL.\'&via=Crunchify\';
$facebookURL = \'https://www.facebook.com/sharer/sharer.php?u=\'.$crunchifyURL;
$googleURL = \'https://plus.google.com/share?url=\'.$crunchifyURL;
$bufferURL = \'https://bufferapp.com/add?url=\'.$crunchifyURL.\'&text=\'.$crunchifyTitle;
$linkedInURL = \'https://www.linkedin.com/shareArticle?mini=true&url=\'.$crunchifyURL.\'&title=\'.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = \'https://pinterest.com/pin/create/button/?url=\'.$crunchifyURL.\'&media=\'.$crunchifyThumbnail[0].\'&description=\'.$crunchifyTitle;
// Add sharing button at the end of page/page content
$variable .= \'<!-- Implement your own social sharing buttons without any JavaScript loading. No plugin required. Detailed steps here: http://crunchify.me/1VIxAsz -->\';
$variable .= \'<div class="crunchify-social">\';
$variable .= \'<a class="crunchify-link crunchify-twitter" href="\'. $twitterURL .\'" target="_blank">Twitter</a>\';
$variable .= \'<a class="crunchify-link crunchify-facebook" href="\'.$facebookURL.\'" target="_blank">Facebook</a>\';
$variable .= \'<a class="crunchify-link crunchify-googleplus" href="\'.$googleURL.\'" target="_blank">Google+</a>\';
$variable .= \'<a class="crunchify-link crunchify-buffer" href="\'.$bufferURL.\'" target="_blank">Buffer</a>\';
$variable .= \'<a class="crunchify-link crunchify-linkedin" href="\'.$linkedInURL.\'" target="_blank">LinkedIn</a>\';
$variable .= \'<a class="crunchify-link crunchify-pinterest" href="\'.$pinterestURL.\'" data-pin-custom="true" target="_blank">Pin It</a>\';
$variable .= \'</div>\';
return $variable.$content;
}else{
// if not a post/page then don\'t include sharing button
return $variable.$content;
}
};
add_filter( \'the_content\', \'crunchify_social_sharing_buttons\');
这是我的分类。php,按要求
<?php
/**
* The Category template file.
*
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package Zeal
*/
get_header(); ?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="container archive-page">
<div class="row">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><h1><?php single_cat_title(); ?></h1>
</header><!-- .page-header -->
<?php
the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
?>
<hr class="wp-block-separator">
<div id="category-menu"><a class="category-link" href="https://www.obsidianurbexphotography.com/locations/">All</a> <a class="category-link" href="https://www.obsidianurbexphotography.com/locations/education/">Education</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/industrial/">Industrial</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/leisure/">Leisure</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/medical/">Medical</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/military/">Military</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/other/">Other</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/religious/">Religious</a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/residential/">Residential</a>
</br>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/austria/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/belgium/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/bulgaria/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/france/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/germany/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/great-britain/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/hungary/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/italy/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/japan/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/luxembourg/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/portugal/"> </a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/romania/"></a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/ukraine/"> </a>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/usa/"></a> </div>
</br>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* Run the loop for the search to output the results.
*/
get_template_part( \'template-parts/content\', \'category\' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( \'template-parts/content\', \'none\' ); ?>
<?php endif; ?>
</div>
</div>
</main><!-- #main -->
</section><!-- #primary -->
<?php get_footer(); ?>
最合适的回答,由SO网友:Antti Koskinen 整理而成
基于您的类别。php我假设您使用的是Zeal主题,这意味着get_template_part( \'template-parts/content\', \'category\' )
可能会吐出这样的东西,https://themes.trac.wordpress.org/browser/zeal/1.0.8/template-parts/content-category.php, 如果我错了,请纠正我。
基于这个假设,我想说没有the_content
钩在你的环中可用,所以你的crunchify_social_sharing_buttons
函数永远不会执行。但你应该能够使用the_excerpt
过滤器,组件the_excerpt
在模板零件文件中使用,以实现相同的结果。要做到这一点,您需要稍微修改您的代码-只需将社交媒体链接生成移动到一个单独的函数,然后将其与这两个函数挂钩the_content
和the_excerpt
.
就像这样,
add_filter( \'the_content\', \'the_content_crunchify_social_sharing_buttons\');
function the_content_crunchify_social_sharing_buttons($content) {
return (is_singular() || is_home()) ? crunchify_social_sharing_buttons($content) : $content;
}
add_filter( \'the_excerpt\', \'the_excerpt_crunchify_social_sharing_buttons\');
function the_excerpt_crunchify_social_sharing_buttons($excerpt) {
return (is_archive()) ? crunchify_social_sharing_buttons($excerpt) : $excerpt;
}
function crunchify_social_sharing_buttons($content) {
global $post;
// Get current page URL
$crunchifyURL = urlencode(get_permalink());
// Get current page title
$crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, \'UTF-8\')), ENT_COMPAT, \'UTF-8\');
// $crunchifyTitle = str_replace( \' \', \'%20\', get_the_title());
// Get Post Thumbnail for pinterest
$crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full\' );
// Construct sharing URL without using any script
$twitterURL = \'https://twitter.com/intent/tweet?text=\'.$crunchifyTitle.\'&url=\'.$crunchifyURL.\'&via=Crunchify\';
$facebookURL = \'https://www.facebook.com/sharer/sharer.php?u=\'.$crunchifyURL;
$googleURL = \'https://plus.google.com/share?url=\'.$crunchifyURL;
$bufferURL = \'https://bufferapp.com/add?url=\'.$crunchifyURL.\'&text=\'.$crunchifyTitle;
$linkedInURL = \'https://www.linkedin.com/shareArticle?mini=true&url=\'.$crunchifyURL.\'&title=\'.$crunchifyTitle;
// Based on popular demand added Pinterest too
$pinterestURL = \'https://pinterest.com/pin/create/button/?url=\'.$crunchifyURL.\'&media=\'.$crunchifyThumbnail[0].\'&description=\'.$crunchifyTitle;
// Add sharing button at the end of page/page content
$variable = \'<div class="crunchify-social">\';
$variable .= \'<a class="crunchify-link crunchify-twitter" href="\'. $twitterURL .\'" target="_blank">Twitter</a>\';
$variable .= \'<a class="crunchify-link crunchify-facebook" href="\'.$facebookURL.\'" target="_blank">Facebook</a>\';
$variable .= \'<a class="crunchify-link crunchify-googleplus" href="\'.$googleURL.\'" target="_blank">Google+</a>\';
$variable .= \'<a class="crunchify-link crunchify-buffer" href="\'.$bufferURL.\'" target="_blank">Buffer</a>\';
$variable .= \'<a class="crunchify-link crunchify-linkedin" href="\'.$linkedInURL.\'" target="_blank">LinkedIn</a>\';
$variable .= \'<a class="crunchify-link crunchify-pinterest" href="\'.$pinterestURL.\'" data-pin-custom="true" target="_blank">Pin It</a>\';
$variable .= \'</div>\';
return $variable.$content;
}