【问题标题】:Access the post data via short code (Visual Composer post grid)通过短代码访问帖子数据(Visual Composer 帖子网格)
【发布时间】:2017-07-19 13:41:50
【问题描述】:

我正在使用带有自定义模板的 Visual Composer(后网格)元素。

我想通过简码输出帖子 ID,所以我创建了一个简单的简码:

   function myshortcode_title( ){
   return get_the_ID();
   }
   add_shortcode( 'page_title', 'myshortcode_title' );

但它似乎没有检索到帖子 ID。我通过文本块将它添加到网格中。

短代码适用于任何其他页面,但不适用于 VC 帖子网格内。

我还能如何访问帖子网格内的帖子 ID?

提前致谢!这是元素的屏幕截图。

【问题讨论】:

    标签: php wordpress post visual-composer


    【解决方案1】:

    我现在已经很晚了,但我遇到了同样的问题,这个答案可以帮助其他人。 您需要在函数中使用全局 $post 。

    使用示例:

    function my_function_shortcode()
    {
        global $post; // important !
    
        $args = array(
            'post_type' => 'event',
            'post_status' => 'publish',
        );
    
        $event_query = new WP_Query($args);
        if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post();
                $event_location = get_post_meta($post->ID, 'event_location', true);
            endwhile;
        endif;
        wp_reset_query();
    }
    
    add_shortcode('my_function_output', 'my_function_shortcode');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2014-11-28
      相关资源
      最近更新 更多