Wordpress短代码通过功能输出

时间:2018-03-22 13:11:36

标签: php wordpress function shortcode

短代码从另一个函数获取输出的最佳方法是什么。 echo工作但它在所有内容之前生成短代码。在结束文件之前有一个PHP开关,我正在加载不同的功能,基于选择的样式。这是功能: staticMarkup($ titleRendered,$ contentRendered,$ feedbackRendered,$ roleRendered,$ companyRendered,$ image_urlRendered,$ slider,$ color);



<?php
add_shortcode('sample', 'sample_shortcode');

function sample_shortcode($atts)
	{
	global $tesio;
	$markup = '';

	// Attributes

	$atts = shortcode_atts(array(
		'style' => '',
		'testio_id' => '',
		'slider' => '',
		'colors' => '',
		'color' => '',
		'wide' => 'no',
		'bgcolor' => '',
		'dpadding' => '',
		'tpadding' => '',
		'spadding' => '',
		'instance' => 1
	) , $atts);
	wp_enqueue_script('testio-front-js');
	wp_localize_script('testio-front-js', 'testio_var', $atts);
	$testio_id = $explodeID = $style = $slider = $colors = $bgcolor = $vpadding = $color = $wide = $colorContrast = '';
	$testio_id = $atts['testio_id'];
	$explodeID = explode(',', $testio_id);
	$style = $atts['style'];
	$slider = $atts['slider'];
	$colors = $atts['colors'];
	$bgcolor = $atts['bgcolor'];
	$dpadding = $atts['dpadding'];
	$tpadding = $atts['tpadding'];
	$spadding = $atts['spadding'];
	$color = $atts['color'];
	$wide = $atts['wide'];
	$testioAmount = count(explode(',', $testio_id));
	if (!empty($colors))
		{
		$colors = explode(',', $colors);
		}

	if (!empty($bgcolor))
		{
		$colorContrast = getContrast50($bgcolor);
		}

	static $uniqueID;
	if ($testio_id !== '')
		{
		if ($testio_id == 'all')
			{
			$args = array(
				'post_type' => 'testio_wp',
				'post_status' => 'publish',
				'posts_per_page' => - 1
			);
			}
		  else
		if ($testioAmount > 1)
			{
			$args = array(
				'post_type' => 'testio_wp',
				'post_status' => 'publish',
				'post__in' => $explodeID,
				'posts_per_page' => $testioAmount,
				'orderby' => 'post__in'
			);
			}
		  else
		if ($testioAmount == 1)
			{
			$args = array(
				'post_type' => 'testio_wp',
				'post_status' => 'publish',
				'p' => $testio_id,
				'posts_per_page' => 1
			);
			}

		$posts = get_posts($args);
		$wrapperClass = 'staticWrap';
		$wideClass = '';
		$sliderClass = '';
		$masonryContainer = '';
		$masonryContainerClose = '';
		$backgroundStyle = '';
		$sliderMarkup = '';
		$sliderCloseMarkup = '';
		$wrapperClassExtra = '';
		$dataAttr = 'data-padding = ' . $dpadding . ',' . $tpadding . ',' . $spadding;
		switch ($style)
			{
		case 'static':
			if ($wide == 'yes')
				{
				$wideClass = ' wide';
				}

		default:
			break;
			}

		if ($testioAmount == 1)
			{
			$slider == 'no';
			$sliderContainer = '';
			}

		$markup.= '<div class="testioWrap ' . $wrapperClass . $wrapperClassExtra . '" ' . $backgroundStyle . ' data-id="' . $wrapperClass . $uniqueID . '" ' . $dataAttr . '>';
		$markup.= $sliderMarkup;
		$markup.= $masonryContainer;
		$x = 0;
		$colorClass = '';
		$postCount = '';
		foreach($posts as $post)
			{
			$titleRendered = $post->post_title;
			$contentRendered = $post->post_content;
			$idRendered = $post->ID;
			$feedbackRendered = get_field('testio_rate', $idRendered);
			$roleRendered = get_field('testio_role', $idRendered);
			$companyRendered = get_field('testio_company', $idRendered);
			$image_urlRendered = get_the_post_thumbnail($idRendered, 'thumbnail');
			if (!empty($colors))
				{
				$colorClass = $colors[$x % 5];
				}

			$x++;
			$postCount++;
			$freeMessage = '<div class="testio-notice"> Please get the pro version to unlock ' . $style . ' style <a href="http://testiowp.com" class="testio_btn">Upgrade</a></div>';
			switch ($style)
				{
			case "static":
				staticMarkup($titleRendered, $contentRendered, $feedbackRendered, $roleRendered, $companyRendered, $image_urlRendered, $slider, $color);
			default:
				break;
				}
			}

		$markup.= $masonryContainerClose;
		$markup.= $sliderCloseMarkup;
		$markup.= '</div>';
		return $markup;
		}
	  else
		{
		$markup.= '<div class="testio">';
		$markup.= __('Please put an id to the shortcode', 'testio-wp');
		$markup.= '</div>';
		}

	$uniqueID++;
	}

?>
&#13;
&#13;
&#13;

这是staticMarkup函数中的代码:

&#13;
&#13;
<?php
function staticMarkup($title, $content, $feedback, $role, $company, $image_url, $slider, $color){
        $markup ='';
        $colorStyle = 'style="color: '.$color.';"';
        if($slider == 'yes'){
            $markup .= '<div>';
        }

        $markup .= '<div class="testio testioStatic" data-color="'.$color.'">';
            $markup .= '<div class="leftBorder"></div>';
            if($image_url !== '' ){
                $markup .= '<div class="thumbnailWrapper">';
                $markup .= $image_url;
                $markup .= '</div>';
            }
            $markup .= feedbackMarkup($feedback);
            $markup .= '<div class="testio-content">';
                $markup .= '<p>'.$content.'</p>';
            $markup .= '</div>';
            $markup .= '<h3 class="testio-name" '.$colorStyle.'>~ ' .$title. '</h3>';
            $markup .= '<h4 class="testio-role">' .$role. ' <span class="role-divider icon-at-sign"></span> ' .$company. '</h4>';
            
        $markup .= '</div>';
        if($slider == 'yes'){
            $markup .= '</div>';
        }
        echo $markup;
    }
 ?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在staticMarkup函数中替换:

echo $markup; 

使用:

return $markup;

这将返回已定位/调用的变量,而不是所有内容。