以下功能通过将其添加到> the_content()来工作,但是,我希望通过自定义标记将其显示在我的html标记中的自定义位置,当我尝试这个时我得到了一个PHP错误,我是确定这是非常简单的事情,但我为我的生活看不到问题。
// code from crunchify
function supersun_social_sharing_buttons($content) {
global $post;
if(is_singular() || is_home()){
// Get current page URL
$supersunURL = urlencode(get_permalink());
// Get current page title
$supersunTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$supersunThumbnail = 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='.$supersunTitle.'&url='.$supersunURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$supersunURL;
$googleURL = 'https://plus.google.com/share?url='.$supersunURL;
$whatsappURL = 'whatsapp://send?text='.$supersunTitle . ' ' . $supersunURL;
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$supersunURL.'&media='.$supersunThumbnail[0].'&description='.$supersunTitle;
// Add sharing button at the end of page/page content
$variable .= '<div class="supersun-social">';
$variable .= '<a class="supersun-social-link supersun-twitter" href="'. $twitterURL .'" target="_blank" title="Share on Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-facebook" href="'.$facebookURL.'" target="_blank" title="Share on Facebook"><i class="fa fa-facebook" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-whatsapp" href="'.$whatsappURL.'" target="_blank" title="Share on Whatsapp"><i class="fa fa-whatsapp" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-googleplus" href="'.$googleURL.'" target="_blank" title="Share on Google+"><i class="fa fa-google-plus" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-pinterest" href="'.$pinterestURL.'" target="_blank" title="Share on Pinterest"><i class="fa fa-pinterest" aria-hidden="true"></i></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', 'supersun_social_sharing_buttons');
我添加时出现此错误消息
<?php supersun_social_sharing_buttons(); ?>
我的主题。
非常感谢你的时间。对不起,该网站目前不在线,我认为对于有经验的人来说这应该是一个简单的解决方法。
<?php
/**
* The template for displaying all single posts and attachments
*
* @package FoundationPress
* @since FoundationPress 1.0.0
*/
get_header(); ?>
<div class="post-container">
<div class="blog-single-header-background" id="blog-single-header-background">
</div>
<?php supersun_social_sharing_buttons($content); ?>
<div class="container post-content-container">
<div class="columns medium-10 medium-centered">
<div itemscope itemtype="http://schema.org/Article" class="single-blog-post" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article <?php post_class('main-content') ?> id="post-<?php the_ID(); ?>">
<header class="post-header">
<h1 itemprop="name" class="entry-title"><?php the_title(); ?></h1>
<div class="post-author-details">
<a href="<?php get_the_author_link(); ?>" class="post-author-avatar image-circle float-left">
<?php echo get_avatar( get_the_author_meta( 'ID' ), 64 ); ?>
</a>
<div class="post-author-meta media-body margin-left">
<h4 class="author-post-byline">
<span> By </span>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span itemprop="name" class="author-post-link"><?php the_author_posts_link(); ?></span>
</span>
<span class="post-date">
on <?php echo the_time("M j, Y");?>
</span>
</h4>
<span class="post-meta-wrap">
<span class="post-category">
<span class="pre-cat"> In
</span>
<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}?>
</span>
</span>
</div>
</div>
<div class="clear"></div>
</header>
<div class="post-content">
<span itemprop="articleBody">
<div class="post-image">
<?php echo the_post_thumbnail( 'large' ); ?>
</div>
<?php the_content(); ?>
</span>
</div>
<footer>
<div class="post-author-about-section">
<?php
global $post;
// Detect if it is a single post with a post author
if ( is_single() && isset( $post->post_author ) ) {
// Get author's display name
$display_name = get_the_author_meta( 'display_name', $post->post_author );
// If display name is not available then use nickname as display name
if ( empty( $display_name ) )
$display_name = get_the_author_meta( 'nickname', $post->post_author );
// Get author's biographical information or description
$user_description = get_the_author_meta( 'user_description', $post->post_author );
// Get author's website URL
$user_website = get_the_author_meta('url', $post->post_author);
// Get link to the author archive page
$user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
if ( ! empty( $user_description ) )
// Author avatar and bio
$author_details = '<a href="' . $user_posts . '" class="author-about-avatar">' . get_avatar( get_the_author_meta('user_email') , 90 ) . '</a>';
$author_details .= '<div class="media-body margin-left">';
if ( ! empty( $display_name ) )
// $author_details = '<p class="author-about-name">About ' . $display_name . '</p>';
$author_details .= '<h4 class="author-post-byline author-about-name">';
$author_details .= '<span class="pre-author-header"> About </span>';
$author_details .= '<span itemprop="author" itemscope itemtype="http://schema.org/Person">';
$author_details .= '<span itemprop="name" class="author-post-link"><a href="' . $user_posts . '">' . $display_name . '</a></span>';
$author_details .= '</span>';
$author_details .= '</h4>';
$author_details .= '<p class="author-about-details">' . nl2br( $user_description ). '</p>';
// $author_details .= '<p class="author-about-links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';
// Check if author has a website in their profile
if ( ! empty( $user_website ) ) {
// Display author website link
$author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>';
} else {
// if there is no author website then just close the paragraph
$author_details .= '</p>';
}
$author_details .= '</div">';
// Pass all this info to post content
$content = $content . $author_details;
}
echo $content;
?>
</div>
</footer>
</article>
</div>
<?php endwhile;?>
<div class="related-posts">
<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>2, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="relatedposts"><h3>Related Posts</h3>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="columns medium-6 large-6 collapse margin-bottom">
<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
<div class="related-post-item content-card">
<div class="related-thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="related-content">
<div class="related-category-title">
<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}?>
</div>
<h2>
<?php the_title(); ?>
</h2>
</div>
</div>
</a>
</div>
<?php }
echo '</div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
</div>
<div class="clear"> </div>
<!-- <h3> Comments </h3> -->
<div class="comments">
<?php do_action( 'foundationpress_post_before_comments' ); ?>
<?php comments_template(); ?>
<?php do_action( 'foundationpress_post_after_comments' ); ?>
</div>
</div>
</div>
</div>
<?php get_footer();
答案 0 :(得分:-1)
您需要在直接调用函数时传递参数,而不是add_filter
。像这样<?php supersun_social_sharing_buttons($parameter); ?>