Php 尝试从id调用shorcode的文章标题、摘录和永久链接
我能够正确显示标题和摘录,但我不知道该使用哪个permalink调用Php 尝试从id调用shorcode的文章标题、摘录和永久链接,php,wordpress,Php,Wordpress,我能够正确显示标题和摘录,但我不知道该使用哪个permalink调用 function display_excerpt_shortcode( $atts ) { extract(shortcode_atts(array( 'excerptid' => '' ), $atts)); if ($excerptid) { $args=array( 'p' => $excerptid, 'post_type' => 'post', '
function display_excerpt_shortcode( $atts ) {
extract(shortcode_atts(array(
'excerptid' => ''
), $atts));
if ($excerptid) {
$args=array(
'p' => $excerptid,
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if ($my_query) {
$title = apply_filters( 'the_title', $my_query->posts[0]->post_title );
$excerpt = apply_filters( 'the_excerpt', $my_query->posts[0]->post_excerpt );
$link = apply_filters( 'the_permalink', $my_query->posts[0]->post_permalink );
return '<h3>' . $title . '</h3> <p>' . $excerpt . '</p> <a class="button small primary" href="' . $link . '" title="' . $title . '" >Read More </a>';
}
}
return;
}
add_shortcode('display_excerpt', 'display_excerpt_shortcode');
功能显示\u摘录\u短代码($atts){
提取(短码)附件(数组)(
“摘录ID”=>“
)美元(附件);;
如果($摘录ID){
$args=数组(
'p'=>节选ID$,
“post_type”=>“post”,
“发布状态”=>“发布”
);
$my\u query=新的WP\u查询($args);
如果($my_query){
$title=apply_过滤器('the_title',$my_query->posts[0]->post_title);
$EXECRPT=apply_过滤器('the_extract',$my_query->posts[0]->post_extract);
$link=apply_过滤器('the_permalink',$my_query->posts[0]->post_permalink);
返回“.$title.”.$extract.”;
}
}
返回;
}
添加_短码(“显示_摘录”、“显示_摘录_短码”);
我试过各种组合。永久链接,获取永久链接,发布永久链接。。。我只是不知道这是错误的组合还是我完全偏离了目标。提前感谢。您是否尝试过:
$link=get\u permalink($my\u query->posts[0]->post\u ID)
我认为您的问题在于查询对象没有“permalink”属性。
按照中的指南,您将找到在循环的每次迭代中使用$the_query->the_post()设置新post对象的模式代码>:
$thew_query=newwp_query($args);
//环路
if($the\u query->have\u posts()){
while($the\u query->have\u posts()){
$the_query->the_post();
//现在参考WP post功能:
_title();
_permalink();
}
}
很抱歉,这是一个完全不同的设计,但我从来没有失败过。这适用于提取所有值,但不幸的是,它会回显而不是返回值,并且不会以我希望的方式从快捷码中显示。事实上,你是完全正确的。你的第一段代码就成功了。谢谢令人惊叹的。对于第二个示例,您将使用“get_the”函数族,如$link=get_the_permalink()代码>
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//Now reference WP post functions:
the_title();
the_permalink();
}
}