dongyu8694 2017-07-24 11:28
浏览 70
已采纳

在短代码中包装一个函数,然后在我的wordpress网站上的编辑器中返回并回显结果

Here's my problem:

Two weeks ago I created a nice function to populate my options in a select element for a wordpress website. My purpose was to add all the names of my members with their emails as values from some posts that I create under the category "Member", so, whenever I edit, add or delete any member in my website, that section will be automatically updated.

Something like:

<select>
<option value="myemail@test.com">First user</option>
<option value="myemail2@test.com">Second user</option>
<option value="myemail3@test.com">Third user</option>
</select>

To achieve my goal I modify the loop in this way:

$members = [];
$temp_query = $wp_query;
query_posts("cat=2&showposts=20");
while (have_posts()) : the_post();
$membername = get_post_meta( $post->ID, 'name', true );
$memberemail = get_post_meta( $post->ID, 'email', true );
if ( ! empty( $membername ) ) {
    $members[] = '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
}
endwhile;

and then I call the options in my selector like that:

<select class="drops" name="smallsan">
    <?php echo implode($members); ?>
</select>

So far so good except for the fact that I wrap this code in my template.

My problem is that I need to run a plugin in order to hide the page from people who are not subscribed so I have to add the html in the loop in the editor and I can't execute php there.

So I tried to wrap the code in my function.php to associate it with a shortcode like that:

function mymembershortcode() {
    $members = [];
    $temp_query = $wp_query;
    query_posts("cat=2&showposts=20");
    while (have_posts()) : the_post();
    $membername = get_post_meta( $post->ID, 'name', true );
    $memberemail = get_post_meta( $post->ID, 'email', true );
    if ( ! empty( $membername ) ) {
        $members[] = '<option value="' . $memberemail . '">' . get_post_meta($post->ID, 'name', true) .'</option>';
    }
    endwhile;
    return implode($members);
}
add_shortcode('mymemberselector', 'mymembershortcode');

To run the shortcode:

<select class="drops" name="smallsan">
    [mymemberselector]
</select>

essentially whenever I need but, it doesn't work, and I suspect it is because I can't return and echo at the same time.

Does anyone know how can I solve my problem?

展开全部

  • 写回答

1条回答 默认 最新

  • dongxie3701 2017-07-24 11:58
    关注

    Try this instead:

    function mymembershortcode() {
        $members = [];
        $query = new WP_Query( array(
            'cat' => 2,
            'posts_per_page' => 20,
        )); 
    
        while ($query->have_posts()) : $query->the_post();
            $membername = get_post_meta( get_the_ID(), 'name', true );
            $memberemail = get_post_meta( get_the_ID(), 'email', true );
            if ( ! empty( $membername ) ) {
                $members[] = '<option value="' . $memberemail . '">' . get_post_meta(get_the_ID(), 'name', true) .'</option>';
            }
    
        endwhile;
        wp_reset_postdata(); 
        return implode($members);
    
    }
    
    add_shortcode('mymemberselector', 'mymembershortcode');
    

    Here's some info about why you should NEVER use query_posts() https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部