dongyu8694 2017-07-24 19: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 19: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

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题