我无法将图像从我的一个字段转换为短代码
(它与get_the_post_thumbnail($post->ID, \'thumbnail\')
但我需要字段中的值udstiller logo
.
function logo_shortcode( $atts ) {
extract( shortcode_atts( array(
\'limit\' => \'50\',
\'orderby\' => \'name\',
), $atts ) );
// Creating custom query to fetch the project type custom post.
$loop = new WP_Query(array(\'post_type\' => \'udstillere\', \'posts_per_page\' => $limit, \'orderby\' => $orderby));
$output = \'<ul class="bxslider">\';
// Looping through the posts and building the HTML structure.
if($loop){
while ($loop->have_posts()){
$loop->the_post();
$output .= \'<li><a href="\'.get_permalink().\'"><img src="\'.get_post_meta($post->ID,\'wpcf-udstiller-logo\',true).\'"/></a></li>\';
}
}
else
$output = \'Sorry, No projects yet. Come back Soon.\';
// Now we are returning the HTML code back to the place from where the shortcode was called.
return $output;
$output = \'</ul>\';
}
add_shortcode("Logo", "logo_shortcode");
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
$post
是全局变量,因此在此范围内不可见(以及使用$post
在您的代码中,您不是指全局$post
变量,但为局部变量)
你应该把global $post
代码中的某个地方。例如:
...
while ($loop->have_posts()){
global $post;
$loop->the_post();
$output .= \'<li><a href="\'.get_permalink().\'"><img src="\'.get_post_meta($post->ID,\'wpcf-udstiller-logo\',true).\'"/></a></li>\';
}
...