简体   繁体   中英

WordPress- Calling a function - not working when using custom tag

The following function works by adding it to >the_content() , however, I want to display it at a custom location in my html markup via a custom tag, when I tried this I got a PHP error, I'm sure this is something very simple but I for the life of me cannot see the issue.

  // 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');

THIS ERROR MESSAGE APPEARS WHEN I ADD <?php supersun_social_sharing_buttons(); ?> <?php supersun_social_sharing_buttons(); ?> TO MY THEME.

在此处输入图片说明

Thank you so much for your time. Sorry the site is not currently live, I think this should be an easy fix for someone with an experienced eye.

 <?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();

You need pass a parameter when you call your function directly, not in add_filter . Like this <?php supersun_social_sharing_buttons($parameter); ?> <?php supersun_social_sharing_buttons($parameter); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM