为什么我的 Wordpress 短代码功能不适用于我的自定义 post 类型?
Why doesn't my Wordpress shortcode function work for my custom post types?
我正在尝试创建一个将显示 post(由用户选择)的简码。我有一个适用于常规 post 的函数,但它不会显示我的自定义 post 类型(或与此相关的页面)的任何内容,我不太清楚为什么。
简码:
[post_feat p=2512]
这是我的短代码:
add_shortcode('post_feat', 'post_shortcode_query');
function post_shortcode_query($atts, $content){
extract(shortcode_atts(array( // a few default values
'posts_per_page' => '1',
'post_type' => array( 'post', 'page', 'product-reviews' ),
), $atts));
global $post;
$posts = new WP_Query($atts);
$output = '';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$out = '<div class="featured-post-menu">
<a href="'.get_permalink().'" title="' . get_the_title() . '">
<div class="ft-img-post">
<img src="'. $feat_image . '">
</div>
<h2 class="aside">'.get_the_title() .'</h2>';
// add here more...
$out .='</a></div>';
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($out);
}
add_filter('widget_text', 'do_shortcode');
这是我的自定义 post 类型代码:
register_post_type( 'product-reviews',
// CPT Options
array(
'labels' => array(
'name' => __( 'Product Reviews' ),
'singular_name' => __( 'Product Review' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'product-reviews'),
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', ),
'taxonomies' => array('topics', 'category' ),
'capability_type' => 'post',
)
);
如果有人知道为什么这对我不起作用,我将非常感谢您的帮助。谢谢。
shortcode_atts 输出一个数组,它不会改变现有的数组。
然后您将提取键作为值。
将代码更改为以下代码以使其正常工作。
add_shortcode('post_feat', 'post_shortcode_query');
function post_shortcode_query($atts, $content){
extract(shortcode_atts(array('p'=>-1), $atts));
$args=array(
'posts_per_page' => '1',
'post_type' => array( 'post', 'page', 'product-reviews' ),
'p'=>$p,
);
global $post;
$posts = new WP_Query($args);
$output = '';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$out = '<div class="featured-post-menu">
<a href="'.get_permalink().'" title="' . get_the_title() . '">
<div class="ft-img-post">
<img src="'. $feat_image . '">
</div>
<h2 class="aside">'.get_the_title() .'</h2>';
// add here more...
$out .='</a></div>';
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($out);
}
我正在尝试创建一个将显示 post(由用户选择)的简码。我有一个适用于常规 post 的函数,但它不会显示我的自定义 post 类型(或与此相关的页面)的任何内容,我不太清楚为什么。
简码:
[post_feat p=2512]
这是我的短代码:
add_shortcode('post_feat', 'post_shortcode_query');
function post_shortcode_query($atts, $content){
extract(shortcode_atts(array( // a few default values
'posts_per_page' => '1',
'post_type' => array( 'post', 'page', 'product-reviews' ),
), $atts));
global $post;
$posts = new WP_Query($atts);
$output = '';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$out = '<div class="featured-post-menu">
<a href="'.get_permalink().'" title="' . get_the_title() . '">
<div class="ft-img-post">
<img src="'. $feat_image . '">
</div>
<h2 class="aside">'.get_the_title() .'</h2>';
// add here more...
$out .='</a></div>';
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($out);
}
add_filter('widget_text', 'do_shortcode');
这是我的自定义 post 类型代码:
register_post_type( 'product-reviews',
// CPT Options
array(
'labels' => array(
'name' => __( 'Product Reviews' ),
'singular_name' => __( 'Product Review' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'product-reviews'),
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', ),
'taxonomies' => array('topics', 'category' ),
'capability_type' => 'post',
)
);
如果有人知道为什么这对我不起作用,我将非常感谢您的帮助。谢谢。
shortcode_atts 输出一个数组,它不会改变现有的数组。
然后您将提取键作为值。
将代码更改为以下代码以使其正常工作。
add_shortcode('post_feat', 'post_shortcode_query');
function post_shortcode_query($atts, $content){
extract(shortcode_atts(array('p'=>-1), $atts));
$args=array(
'posts_per_page' => '1',
'post_type' => array( 'post', 'page', 'product-reviews' ),
'p'=>$p,
);
global $post;
$posts = new WP_Query($args);
$output = '';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$out = '<div class="featured-post-menu">
<a href="'.get_permalink().'" title="' . get_the_title() . '">
<div class="ft-img-post">
<img src="'. $feat_image . '">
</div>
<h2 class="aside">'.get_the_title() .'</h2>';
// add here more...
$out .='</a></div>';
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($out);
}