Php 获取\u在短代码中不起作用的\u内容
我为wordpress写的一个短代码有问题。我试图使用Php 获取\u在短代码中不起作用的\u内容,php,wordpress,shortcode,Php,Wordpress,Shortcode,我为wordpress写的一个短代码有问题。我试图使用获取内容()函数,但它不是拉取我创建的自定义帖子类型的内容,而是拉取快捷码所在页面的内容。其他功能都工作正常,例如获取标题()和g设置帖子缩略图()。我在函数中传递ID,它对其他所有功能都很有效,只是不能获取内容 get_the_content($testimonial_item->ID); 短码包含分页和其他元素,它们都能正常工作,正是这一个功能让我感到悲伤。完整的代码如下,任何帮助都将不胜感激 function ncweb_dis
获取内容()
函数,但它不是拉取我创建的自定义帖子类型的内容,而是拉取快捷码所在页面的内容。其他功能都工作正常,例如获取标题()
和g设置帖子缩略图()
。我在函数中传递ID,它对其他所有功能都很有效,只是不能获取内容
get_the_content($testimonial_item->ID);
短码包含分页和其他元素,它们都能正常工作,正是这一个功能让我感到悲伤。完整的代码如下,任何帮助都将不胜感激
function ncweb_display_testimonial_items($atts, $content = null) {
extract( shortcode_atts( array(
'per_page' => 6
), $atts ) );
/* pagination parameters */
// check what page we are on
if ( isset ($_GET['testimonial-page'] ) ) $page = $_GET['testimonial-page']; else $page = 1;
// default number of pages
$total_pages = 1;
// portfolio offset. Used in the get_posts() query to show only portfolio for the current page
$offset = $per_page * ($page-1);
// setup the portfolio args for retrieving the total number of portfolio items
$testimonial_count_args = array(
'post_type' => 'ncweb-testimonials',
'posts_per_page' => -1
);
$testimonial_count = count(get_posts($testimonial_count_args));
// calculate the total number of pages
$total_pages = ceil($testimonial_count/$per_page);
/* end pagination parameters */
// main image query
$testimonial_args = array(
'post_type' => 'ncweb-testimonials',
'numberposts' => $per_page,
'offset' => $offset
);
$testimonial_items = get_posts($testimonial_args);
// start our output buffer
ob_start();
if($testimonial_items) :
/*** main portfolio loop ***/
$counter = 1;
echo '<div class="testimonial-items" id="testimonial-items">';
foreach($testimonial_items as $testimonial_item) :
$testimonial_company = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_company', true);
$testimonial_client = get_post_meta($testimonial_item->ID, 'ncweb_testimonial_client_name', true);
echo '<aside class="testimonial-list-item row">';
echo '<div class="col-xs-12 testimonial-list-item-info">';
echo '<div class="testimonial-image">'. get_the_post_thumbnail($testimonial_item->ID) .'</div>';
echo '<div class="testimonial-client"><span class="testimonial-client-name">'. $testimonial_client .'</span><br/><span class="testimonial-company">'. $testimonial_company .'</span></div>';
echo '</div>'; //end of testimonial-list-item-info
echo '<div class="col-xs-12 testimonial-item-content">'. get_the_content($testimonial_item->ID); .'</div>';
echo '</aside>';
$counter++;
endforeach;
echo '</div>';
/*** display pagination ***/
// pagination base
echo '<div id="testimonial-pagination">';
$base = get_permalink(get_the_ID()) . '%_%';
echo paginate_links( array(
'base' => $base,
'format' => '?testimonial-page=%#%',
'prev_text' => __('Previous', 'ncweb'),
'next_text' => __('Next', 'ncweb'),
'total' => $total_pages,
'current' => $page,
'end_size' => 1,
'mid_size' => 5
));
echo '</div>';
/*** end pagination display ***/
else :
echo '<p>' . __('No testimonial items found.', 'ncweb') . '</p>';
endif; // end if($images)
return ob_get_clean();
}
add_shortcode( 'testimonial-items', 'ncweb_display_testimonial_items' );
功能ncweb\u显示\u推荐项目($atts,$content=null){
提取(短码)附件(数组)(
“每页”=>6
)美元(附件);;
/*分页参数*/
//检查我们在哪一页
if(isset($_-GET['commential-page']))$page=$_-GET['commential-page'];else$page=1;
//默认页数
$total_pages=1;
//公文包偏移量。在get_posts()查询中用于仅显示当前页面的公文包
$offset=$per_page*($page-1);
//设置公文包参数以检索公文包项目的总数
$Estimational\u count\u args=数组(
“post_type”=>“ncweb推荐信”,
“每页帖子”=>-1
);
$ESTIONAL_count=count(获取帖子($ESTIONAL_count_args));
//计算总页数
$total_pages=ceil($commential_count/$per_pages);
/*结束分页参数*/
//主图像查询
$ESTIONAL_args=数组(
“post_type”=>“ncweb推荐信”,
“numberposts”=>每页$,
“偏移量”=>$offset
);
$ESTIONAL_items=获取_帖子($ESTIONAL_args);
//启动输出缓冲区
ob_start();
如果($推荐项目):
/***主投资组合循环***/
$counter=1;
回声';
foreach($推荐项目作为$推荐项目):
$Certional_company=get_post_meta($Certional_item->ID,'ncweb_Certional_company',true);
$testional\u client=get\u post\u meta($testional\u item->ID,'ncweb\u testional\u client\u name',true);
回声';
回声';
回显“”。获取帖子缩略图($Certificational\u item->ID)。“”;
echo“.$ESTIONAL_客户”。
“.$ESTIONAL_公司”;
echo“”;//证明列表项信息的结尾
回显“”。获取内容($commential_item->ID);
回声';
$counter++;
endforeach;
回声';
/***显示分页***/
//分页基
回声';
$base=get_permalink(get_the_ID())。%;
回显分页链接(数组(
'base'=>base美元,
'格式'=>'?推荐页=%#%',
'prev_text'=>'('Previous','ncweb'),
'下一个文本'=>''next','ncweb'),
“总计”=>$total\u页,
“当前”=>$page,
“结束大小”=>1,
“中等尺寸”=>5
));
回声';
/***结束分页显示***/
其他:
回显“”。“(未找到任何证明项目),“ncweb”)。”;
endif;//end if($images)
返回ob_get_clean();
}
添加快捷代码(“推荐项目”、“ncweb显示推荐项目”);
这是因为get\u the_content()
函数是一个包装器,它的唯一参数是Read More
文本和striptaster
参数
你真正想要的是
$post= get_post($testimonial_item->ID);
$testimonial_items = $post->content ;
$testimonial_items = apply_filters('the_content', $testimonial_items);
然后,您将获得所需的内容。在内容中使用快捷码时,您可以尝试使用此代码以获得完美的输出
使用快捷码输出内容最干净的方法是:
function my_sc_content() {
ob_start();
$my_content = the_content();
echo $my_content;
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
add_shortcode('my-content','my_sc_content');
这段代码是正确的,但您应该将$commential_items作为apply_filters()的第二个参数传递,而不是$content