【问题标题】:WordPress- Calling a function - not working when using custom tagWordPress-调用函数-使用自定义标签时不起作用
【发布时间】:2017-06-17 21:31:58
【问题描述】:

以下函数通过将其添加到 >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');

添加时出现此错误消息 &lt;?php supersun_social_sharing_buttons(); ?&gt; 到我的主题。

非常感谢您抽出宝贵时间。抱歉,该网站目前还没有上线,我认为这对于有经验的人来说应该是一个简单的解决方案。

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

【问题讨论】:

  • 你能读懂你得到的错误吗?它说什么? “失踪……”?
  • 是的,请原谅我的经验不足,我不完全确定这意味着什么?任何帮助将不胜感激。

标签: php wordpress function filter tags


【解决方案1】:

直接调用函数时需要传递参数,而不是add_filter。赞这个&lt;?php supersun_social_sharing_buttons($parameter); ?&gt;

【讨论】:

  • 我明白了,但是在这种情况下参数是什么?抱歉,如果我觉得我很笨,我还在学习 - 我尝试了 $content,但这没有用
  • 你的函数获取参数,转换它(或不转换)并返回。当您从add_filter 调用它时,参数是帖子的内容。当你直接调用它时,你必须自己传递一些参数。例如:&lt;?php $text = 'some data'; $changed_text = supersun_social_sharing_buttons($text); ?&gt;
  • 嗯,好的,但是参数必须是具体的吗?我的意思是在我的大脑中我想调用 $content 因为这是我的函数的内容,我会用什么替换“一些数据”以显示我的函数内容?
  • 我不明白你想要实现什么。如果您的代码有效,则调用add_filter( 'the_content', 'supersun_social_sharing_buttons'); 足以将其应用于您的主题。没有必要再次调用它。如果您没有看到内容更改,请在功能中找到问题)
  • 好吧,我想将它显示在我的标记中除“the_content”之外的位置 - 我希望创建一个自定义标签并将其插入到我需要它的时间和地点......这可以实现吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2012-01-23
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多