2017-02-01 99 views
-1

下面的函数通过将其添加到> the_content(),但是,我希望通过自定义标记在我的html标记中的自定义位置显示它,当我试过这个我得到了一个PHP错误,我确定这是非常简单的事情,但我对我的生活看不到问题。WordPress-调用函数 - 使用自定义标记时不工作

// 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(); ?> TO MY的主题。

enter image description here

谢谢你这么多的时间。对不起,该网站目前不在线,我认为这对于有经验的人来说应该是一个简单的解决方案。

<?php 
 
/** 
 
* The template for displaying all single posts and attachments 
 
* 
 
* @package FoundationPress 
 
* @since FoundationPress 1.0.0 
 
*/ 
 

 
get_header(); ?> 
 

 
<div class="post-container"> 
 
\t <div class="blog-single-header-background" id="blog-single-header-background"> 
 
\t </div> 
 
\t \t <?php supersun_social_sharing_buttons($content); ?> 
 
\t <div class="container post-content-container"> 
 
\t \t <div class="columns medium-10 medium-centered"> 
 
\t \t \t <div itemscope itemtype="http://schema.org/Article" class="single-blog-post" role="main"> 
 
\t \t \t \t <?php while (have_posts()) : the_post(); ?> 
 

 
\t \t \t \t \t <article <?php post_class('main-content') ?> id="post-<?php the_ID(); ?>"> 
 
\t \t \t \t \t \t <header class="post-header"> 
 
\t \t \t \t \t \t \t <h1 itemprop="name" class="entry-title"><?php the_title(); ?></h1> 
 

 
\t \t \t \t \t \t \t <div class="post-author-details"> 
 

 
\t \t \t \t \t \t \t \t <a href="<?php get_the_author_link(); ?>" class="post-author-avatar image-circle float-left"> 
 
\t \t \t \t \t \t \t \t \t <?php echo get_avatar(get_the_author_meta('ID'), 64); ?> 
 
\t \t \t \t \t \t \t \t </a> 
 

 
\t \t \t \t \t \t \t \t <div class="post-author-meta media-body margin-left"> 
 

 
\t \t \t \t \t \t \t \t \t <h4 class="author-post-byline"> 
 
\t \t \t \t \t \t \t \t \t \t <span> By </span> 
 
\t \t \t \t \t \t \t \t \t \t <span itemprop="author" itemscope itemtype="http://schema.org/Person"> 
 
\t \t \t \t \t \t \t \t \t \t \t <span itemprop="name" class="author-post-link"><?php the_author_posts_link(); ?></span> 
 
\t \t \t \t \t \t \t \t \t \t </span> 
 
\t \t \t \t \t \t \t \t \t \t <span class="post-date"> 
 
\t \t \t \t \t \t \t \t \t \t \t on <?php echo the_time("M j, Y");?> 
 
\t \t \t \t \t \t \t \t \t \t </span> 
 
\t \t \t \t \t \t \t \t \t </h4> 
 

 
\t \t \t \t \t \t \t \t \t <span class="post-meta-wrap"> 
 
\t \t \t \t \t \t \t \t \t \t \t <span class="post-category"> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <span class="pre-cat"> In 
 
\t \t \t \t \t \t \t \t \t \t \t \t </span> 
 
\t \t \t \t \t \t \t \t \t \t \t \t <?php $categories = get_the_category(); 
 
\t \t \t \t \t \t \t \t \t \t \t \t if (! empty($categories)) { 
 
\t \t \t \t \t \t \t \t \t \t \t \t  echo '<a href="' . esc_url(get_category_link($categories[0]->term_id)) . '">' . esc_html($categories[0]->name) . '</a>'; 
 
\t \t \t \t \t \t \t \t \t \t \t \t }?> 
 
\t \t \t \t \t \t \t \t \t \t \t </span> 
 
\t \t \t \t \t \t \t \t \t </span> 
 

 
\t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t <div class="clear"></div> 
 
\t \t \t \t \t \t </header> 
 

 

 

 
\t \t \t \t \t \t 
 

 

 
\t \t \t \t \t \t <div class="post-content"> 
 
\t \t \t \t \t \t \t <span itemprop="articleBody"> 
 
\t \t \t \t \t \t \t \t <div class="post-image"> 
 
\t \t \t \t \t \t \t \t \t <?php echo the_post_thumbnail('large'); ?> 
 
\t \t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t \t \t <?php the_content(); ?> 
 
\t \t \t \t \t \t \t </span> 
 
\t \t \t \t \t \t </div> 
 

 
\t \t \t \t \t \t <footer> 
 
\t \t \t \t \t \t \t <div class="post-author-about-section"> 
 

 
\t \t \t \t \t \t \t \t <?php 
 
\t \t \t \t \t \t \t global $post; 
 

 
\t \t \t \t \t \t \t // Detect if it is a single post with a post author 
 
\t \t \t \t \t \t \t if (is_single() && isset($post->post_author)) { 
 

 
\t \t \t \t \t \t \t // Get author's display name 
 
\t \t \t \t \t \t \t $display_name = get_the_author_meta('display_name', $post->post_author); 
 

 
\t \t \t \t \t \t \t // If display name is not available then use nickname as display name 
 
\t \t \t \t \t \t \t if (empty($display_name)) 
 
\t \t \t \t \t \t \t $display_name = get_the_author_meta('nickname', $post->post_author); 
 

 
\t \t \t \t \t \t \t // Get author's biographical information or description 
 
\t \t \t \t \t \t \t $user_description = get_the_author_meta('user_description', $post->post_author); 
 

 
\t \t \t \t \t \t \t // Get author's website URL 
 
\t \t \t \t \t \t \t $user_website = get_the_author_meta('url', $post->post_author); 
 

 
\t \t \t \t \t \t \t // Get link to the author archive page 
 
\t \t \t \t \t \t \t $user_posts = get_author_posts_url(get_the_author_meta('ID' , $post->post_author)); 
 

 

 

 
\t \t \t \t \t \t \t if (! empty($user_description)) 
 
\t \t \t \t \t \t \t // Author avatar and bio 
 

 
\t \t \t \t \t \t \t \t $author_details = '<a href="' . $user_posts . '" class="author-about-avatar">' . get_avatar(get_the_author_meta('user_email') , 90) . '</a>'; 
 

 
\t \t \t \t \t \t \t \t $author_details .= '<div class="media-body margin-left">'; 
 

 
\t \t \t \t \t \t \t \t if (! empty($display_name)) 
 

 
\t \t \t \t \t \t \t // $author_details = '<p class="author-about-name">About ' . $display_name . '</p>'; 
 

 
\t \t \t \t \t \t \t \t $author_details .= '<h4 class="author-post-byline author-about-name">'; 
 
\t \t \t \t \t \t \t \t $author_details .= \t '<span class="pre-author-header"> About </span>'; 
 
\t \t \t \t \t \t \t \t $author_details .= \t '<span itemprop="author" itemscope itemtype="http://schema.org/Person">'; 
 
\t \t \t \t \t \t \t \t $author_details .= \t '<span itemprop="name" class="author-post-link"><a href="' . $user_posts . '">' . $display_name . '</a></span>'; 
 
\t \t \t \t \t \t \t \t $author_details .= \t '</span>'; 
 
\t \t \t \t \t \t \t \t $author_details .= '</h4>'; 
 

 
\t \t \t \t \t \t \t $author_details .= '<p class="author-about-details">' . nl2br($user_description). '</p>'; 
 

 
\t \t \t \t \t \t \t // $author_details .= '<p class="author-about-links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>'; 
 

 
\t \t \t \t \t \t \t // Check if author has a website in their profile 
 
\t \t \t \t \t \t \t if (! empty($user_website)) { 
 

 
\t \t \t \t \t \t \t // Display author website link 
 
\t \t \t \t \t \t \t $author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>'; 
 

 
\t \t \t \t \t \t \t } else { 
 
\t \t \t \t \t \t \t // if there is no author website then just close the paragraph 
 
\t \t \t \t \t \t \t $author_details .= '</p>'; 
 
\t \t \t \t \t \t \t } 
 

 
\t \t \t \t \t \t \t \t $author_details .= '</div">'; 
 

 
\t \t \t \t \t \t \t // Pass all this info to post content 
 
\t \t \t \t \t \t \t $content = $content . $author_details; 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t echo $content; 
 
    \t \t \t \t \t \t \t ?> 
 

 

 
\t \t \t \t \t \t \t </div> 
 

 

 

 
\t \t \t \t \t \t </footer> 
 

 
\t \t \t \t \t </article> 
 
\t \t \t </div> 
 
\t \t \t <?php endwhile;?> 
 

 

 

 
\t \t \t <div class="related-posts"> 
 
\t \t \t \t <?php $orig_post = $post; 
 
\t \t \t \t global $post; 
 
\t \t \t \t $tags = wp_get_post_tags($post->ID); 
 
\t \t \t \t if ($tags) { 
 
\t \t \t \t $tag_ids = array(); 
 
\t \t \t \t foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; 
 
\t \t \t \t $args=array(
 
\t \t \t \t 'tag__in' => $tag_ids, 
 
\t \t \t \t 'post__not_in' => array($post->ID), 
 
\t \t \t \t 'posts_per_page'=>2, // Number of related posts that will be shown. 
 
\t \t \t \t 'caller_get_posts'=>1 
 
\t \t \t \t); 
 
\t \t \t \t $my_query = new wp_query($args); 
 
\t \t \t \t if($my_query->have_posts()) { 
 

 
\t \t \t \t echo '<div id="relatedposts"><h3>Related Posts</h3>'; 
 

 
\t \t \t \t while($my_query->have_posts()) { 
 
\t \t \t \t $my_query->the_post(); ?> 
 

 
\t \t \t \t <div class="columns medium-6 large-6 collapse margin-bottom"> 
 
\t \t \t \t \t <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"> 
 
\t \t \t \t \t \t <div class="related-post-item content-card"> 
 

 
\t \t \t \t \t \t \t <div class="related-thumb"> 
 
\t \t \t \t \t \t \t \t \t <?php the_post_thumbnail(); ?> 
 
\t \t \t \t \t \t \t </div> 
 

 
\t \t \t \t \t \t \t <div class="related-content"> 
 

 
\t \t \t \t \t \t \t \t <div class="related-category-title"> 
 
\t \t \t \t \t \t \t \t \t <?php $categories = get_the_category(); 
 
\t \t \t \t \t \t \t \t \t \t \t if (! empty($categories)) { 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t echo esc_html($categories[0]->name); 
 
\t \t \t \t \t \t \t \t \t \t \t }?> 
 
\t \t \t \t \t \t \t \t </div> 
 

 
\t \t \t \t \t \t \t \t <h2> 
 
\t \t \t \t \t \t \t \t \t <?php the_title(); ?> 
 
\t \t \t \t \t \t \t \t </h2> 
 

 
\t \t \t \t \t \t \t </div> 
 

 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </a> 
 

 
\t \t \t \t \t </div> 
 

 
\t \t \t \t <?php } 
 
\t \t \t \t echo '</div>'; 
 
\t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t \t $post = $orig_post; 
 
\t \t \t \t wp_reset_query(); ?> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="clear"> </div> 
 

 
\t \t \t <!-- <h3> Comments </h3> --> 
 

 
\t \t \t <div class="comments"> 
 
\t \t \t \t <?php do_action('foundationpress_post_before_comments'); ?> 
 
\t \t \t \t <?php comments_template(); ?> 
 
\t \t \t \t <?php do_action('foundationpress_post_after_comments'); ?> 
 
\t \t \t </div> 
 

 
\t \t </div> 
 
\t </div> 
 
</div> 
 

 

 

 
<?php get_footer();

+0

你能读出你得到的错误吗?它说什么? “失踪...”? – yivi

+0

是的,请原谅我的经验不足,我不完全确定它是什么意思?任何帮助将非常感激。 –

回答

-1

你需要传递一个参数,当你直接打电话给你的功能,而不是在add_filter。像这样<?php supersun_social_sharing_buttons($parameter); ?>

+0

我明白了,但在这种情况下参数是什么?对不起,如果我遇到愚蠢的,我还在学习 - 我试过$内容,但这不起作用 –

+0

你的函数获取参数,从它转换(或不)并返回。当你从'add_filter'调用它时,参数是post的内容。当你直接调用它时,必须由你自己传递一些参数。例如: '<?php $ text ='some data'; $ changed_text = supersun_social_sharing_buttons($ text); ?>' – questlooking

+0

啊好的,但参数必须具体吗?我的意思是在我的大脑中,我想调用$ content,因为这是我函数的内容,为了让我的函数内容能够显示,我将用什么替换“某些数据”? –