【问题标题】:WordPress Getting post data for Post Grid of Visual ComposerWordPress 为 Visual Composer 的 Post Grid 获取帖子数据
【发布时间】:2015-07-02 23:40:48
【问题描述】:

我在 WordPress 中使用 Visual Composer,我想制作一个自定义的 Post Grid。但是后网格提供的默认元素是不够的。我想显示帖子的作者、它拥有的 cmets 数量、它拥有的类别以及它拥有的标签。我对 Visual Composer 不是很熟悉,但我需要一个正确的方向来获取这些数据?我能做些什么?我搜索了他们的文件,但没有运气。如果我需要移动 php 代码,我想知道我移动的是正确的。有任何想法吗?如果您需要更多信息,请询问:D

提前感谢您的帮助。

【问题讨论】:

    标签: php wordpress blogs


    【解决方案1】:

    如果有人仍在寻找如何在帖子网格中获取 id 或为网格创建特定小部件,您可以使用以下 sn-p I creatd 在帖子标题前添加图标。您将在第一个函数中看到您可以调用 $post-ID 以用于任何查询。

    //** Case Study Title Block Shortcodes ***********
    //********************************
    add_filter( 'vc_gitem_template_attribute_case_study_title','vc_gitem_template_attribute_case_study_title', 10, 2 );
    function vc_gitem_template_attribute_case_study_title( $value, $data ) {
       extract( array_merge( array(
          'post' => null,
          'data' => '',
       ), $data ) );
      $atts_extended = array();
      parse_str( $data, $atts_extended );
      $atts = $atts_extended['atts'];
      // write all your widget code in here using queries etc
      $title = get_the_title($post->ID);
      $link = get_permalink($post->ID);
      $terms = get_the_terms($post->ID, 'case_categories');
      $output = "<h4 class=\"case-title\"><a href=\"". $link ."\">".  get_the_icon($terms[0]->term_id) . $title ."</a></h4>";
      return $output;
    }
    
    add_filter( 'vc_grid_item_shortcodes', 'case_study_title_shortcodes' );
    function case_study_title_shortcodes( $shortcodes ) {
       $shortcodes['vc_case_study_title'] = array(
         'name' => __( 'Case Study Title', 'sage' ),
         'base' => 'vc_case_study_title',
         'icon' => get_template_directory_uri() . '/assets/images/icon.svg',
         'category' => __( 'Content', 'sage' ),
         'description' => __( 'Displays the case study title with correct icon', 'sage' ),
         'post_type' => Vc_Grid_Item_Editor::postType()
      );
      return $shortcodes;
     }
    
    add_shortcode( 'vc_case_study_title', 'vc_case_study_title_render' );
    function vc_case_study_title_render($atts){
       $atts = vc_map_get_attributes( 'vc_case_study_title', $atts );
       return '{{ case_study_title }}';
    }
    

    【讨论】:

    • 欢迎来到 StackOverflow。当您在答案中发布代码时,请详细说明它的作用。有关示例,请参见之前发布的答案。
    • 对我来说效果很好......!!谢谢
    • 终于!这是为 Post Grid 添加自定义短代码的正确方法。在简码中直接使用模板变量 {{ post_data:ID }} 是不够的,它会产生错误。我们必须创建一个自定义变量,然后获取 $post->ID 并在自定义“vc_gitem_template_attribute_”过滤器中操作代码。
    【解决方案2】:

    我遇到了同样的问题;这是我的解决方法:

    根据 Visual Composer 文档:https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/

    当您将以下代码添加到functions.php 时,将在您的自定义网格构建器中添加一个新组件(在我的示例中,名称将是“作者”)。您可以通过发布数据模板变量函数获得许多值,其中一个不是作者的姓名,而是他们的 ID(这很可悲,但至少您可以使用此值来获取作者姓名)。 值为作者的'post_author' =&gt; ID(例如'1')

    这是我获取帖子作者并显示它的功能(如果作者组件已添加到“自定义网格构建器”中的自定义网格中)。把它放在你孩子主题的functions.php

    add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
    function my_module_add_grid_shortcodes( $shortcodes ) {
        $shortcodes['vc_post_id'] = array(
            'name' => __( 'Author', 'my-text-domain' ),
            'base' => 'vc_post_id',
            'category' => __( 'Content', 'my-text-domain' ),
            'description' => __( 'Show current post author', 'my-text-domain' ),
            'post_type' => Vc_Grid_Item_Editor::postType(),
        );
    
    
        return $shortcodes;
    }
    
    // output function
    add_shortcode( 'vc_post_id', 'vc_post_id_render' );
    function vc_post_id_render() {
        $nn = '{{ post_data:post_author }}'; // usage of template variable post_data with argument "post_author"
    
        return get_the_author($nn);
    
    }
    

    有个小问题。它可以工作,但 get_the_author 是 WordPress 中已弃用的功能。我很感激任何使它更现代的建议,或者如果您提出其他替代方案,请提出建议。

    此外,这里是来自文档的vc_post_id_render 的可用变量列表。他们在这里:

    WP_Post::__set_state(array(
       'ID' => 69,
       'post_author' => '1',
       'post_date' => '2015-04-29 14:15:56',
       'post_date_gmt' => '2015-04-29 14:15:56',
       'post_content' => 'Your post content',
       'post_title' => 'Your post title',
       'post_excerpt' => '',
       'post_status' => 'publish',
       'comment_status' => 'open',
       'ping_status' => 'open',
       'post_password' => '',
       'post_name' => 'post name',
       'to_ping' => '',
       'pinged' => '',
       'post_modified' => '2015-06-17 11:18:41',
       'post_modified_gmt' => '2015-06-17 11:18:41',
       'post_content_filtered' => '',
       'post_parent' => 0,
       'guid' => 'http://wp.master/?p=69',
       'menu_order' => 0,
       'post_type' => 'post',
       'post_mime_type' => '',
       'comment_count' => '0',
       'filter' => 'raw',
       'filter_terms' =>
      array (
      ),
    ))
    

    【讨论】:

    • 这里是如何获得作者如果你了解这个基础你也可以获得评论数和你需要的其他数据。要获取评论计数,只需在输出函数中更改 post_data:comment_count
    【解决方案3】:

    这是你的回应

    https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/

    在模板变量的使用中

    例如,在可视化作曲家模板中,您可以使用 {{ post_date:ID }} 来显示帖子 ID。我不知道如何显示标签。

    【讨论】:

    • 是的,我使用帖子作者,但它只显示 id。我不能从那里得到名字。
    • 我认为您可以使用@Hipopeur 回答来获取ID,然后: get_userdata( $userid ) 来获取包含所有用户数据的 WP 对象。
    • @JurgenFeuchter 嗨。我遇到了类似的问题,我需要获取用户 ID,但不使用 {{ post_date:ID }} ,而是作为变量。我需要在另一个简码上使用它,这个值是在渲染后创建的。知道如何获取 ID 吗??
    • 我也需要这个作为变量!不知道该怎么做。有人吗?
    • 还是没有解决办法? {{ ID:ID }} 有效...但是,是的,不知道如何将其作为变量调用以添加一些额外内容以用于内置短代码。
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    相关资源
    最近更新 更多