Custom field in a shortcode?

时间:2013-06-26 作者:zebrastribe

我无法将图像从我的一个字段转换为短代码
(它与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");

2 个回复
最合适的回答,由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>\';
}
...

SO网友:TheDeadMedic

虽然@Krzysiek Dródż的答案很好,但我建议只使用get_the_ID() 而不是$post->ID.

结束

相关推荐

Shortcode of a function

我得到了这个函数,我想将“foreach”的contents divs作为return。我该怎么做?<?php $args = array( \'numberposts\' => 6, \'post_status\'=>\"publish\",\'post_type\'=>\"post\",\'orderby\'=>\"post_date\"); $postslist = get_posts( $args ); fo