首页 文章

Wordpress - 如何在自定义Visual Composer元素中指定ID

提问于
浏览
0

我目前正在为一个非盈利组织 Build 一个网站,并且正在使用旧版的Wordpress主题,Savior .

有一个自定义的帖子类型Donations,它允许管理员设置不同的"Causes",每个都是一个具有特定ID的帖子 .

默认情况下,主题具有自定义Visual Composer元素(屏幕截图1 - https://i.stack.imgur.com/cmhTu.png),允许您显示最新的"Cause";您拥有的唯一自定义选项是VC元素的 Headers .

I'm trying to update this custom VC element so that the admin can specify the exact ID for the "Cause" they would like to feature on a page/post vs. only showing the latest "Cause."

我已经调整了cause.php模板的VC映射,如下所示:

<?php

class STM_VC_Causes {

    function __construct() {
        add_action( 'init', array( $this, 'integrateWithVC' ) );
        add_shortcode( 'stm_causes', array( $this, 'render' ) );
    }

    public function integrateWithVC() {
        if ( function_exists( 'vc_map' ) ) {
            vc_map( array(
                'name'        => __( 'Causes', STM_DOMAIN ),
                'base'        => 'stm_causes',
                'category'    => __( 'STM', STM_DOMAIN ),
                'params'      => array(
                    array(
                        'type' => 'textfield',
                        'class' => '',
                        'heading' => __( 'Title', STM_DOMAIN ),
                        'param_name' => 'title',
                        'value' => __( 'Our Causes', STM_DOMAIN )
                    ),

                    /** ========================================
                     * Qing Custom 8-6-2018
                     * Allow admin to select Cause to feature
                    ======================================== **/

                    array(
                        'type' => 'textfield',
                        'class' => '',
                        'heading' => __( 'Cause ID', STM_DOMAIN ),
                        'param_name' => 'id',
                        'value' => __( '', STM_DOMAIN )
                    )
                )
            ) );
        }
    }


    public function render( $atts, $content = null ) {

        /** ========================================
         * Qing Custom 8-6-2018
         * Display selected Cause ID
        ======================================== **/

        $title = '';
        $id = '';

        extract( shortcode_atts( array(
            'title'       => '',
            'id'       => ''
        ), $atts ) );

        $fea_cause = $atts['id'];
        $donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => $fea_cause ) );

        $output = '';
        $output .= '<ul class="donation-grid first clearfix">';
        while ( $donations->have_posts() ) {

            $donations->the_post();
            $target_amount         = ( get_post_meta( get_the_ID(), 'donation_target', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_target', true );
            $raised_amount         = ( get_post_meta( get_the_ID(), 'donation_raised', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_raised', true );
            $currency              = ( get_post_meta( get_the_ID(), 'donation_currency', true ) == '' ) ? '$' : get_post_meta( get_the_ID(), 'donation_currency', true );
            $donors                = ( get_post_meta( get_the_ID(), 'donation_donors', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_donors', true );
            $target_amount_percent = ( $raised_amount / $target_amount ) * 100;

            $output .= '<li id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">';
            $output .= '<div class="donation-thumbnail">';
            $output .= '<a href="' . get_the_permalink() . '">';
            if ( has_post_thumbnail() ) {
                $output .= get_the_post_thumbnail( get_the_ID(), 'thumb-150x150' );
            }
            $output .= '</a>';
            $output .= '</div>';
            $output .= '<div class="donation-content">';
            $output .= '<h4><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h4>';
            $output .= '<div class="progress_bar"><span style="width: ' . $target_amount_percent . '%;"></span></div>';
            $output .= '<div class="donation-stat">';
            $output .= '<span><i class="fa fa-child"></i> ' . __( 'Raised', STM_DOMAIN ) . '
' . $currency . $raised_amount . '</span>'; $output .= '<span><i class="fa fa-users"></i> ' . __( 'Donors', STM_DOMAIN ) . '
' . $donors . '</span>'; $output .= '<span><i class="fa fa-thumbs-up"></i> ' . __( 'Goal', STM_DOMAIN ) . '
' . $currency . $target_amount . '</span>'; $output .= '</div>'; $output .= '<div class="donate_now">'; $output .= '<a href="' . get_the_permalink() . '" class="button cause_donate_btn">' . __( 'DONATE NOW', STM_DOMAIN ) . '</a>'; $output .= '</div>'; $output .= '</div>'; $output .= '</li>'; } $output .= '</ul>'; wp_reset_query(); return $output; } } if( defined( 'WPB_VC_VERSION' ) ){ new STM_VC_Causes(); } ?>

自定义VC元素在网站的管理后端方面正确显示(截图2 - https://i.stack.imgur.com/zKCgs.png),但我无法弄清楚如何让管理员输入的ID显示在前端 - 无论如何,它仍然显示最新的"Cause."屏幕截图3(https://i.stack.imgur.com/13Qw4.png)只是个人"Cause"在具有自定义VC元素的页面被推送时的样子截图 .

我已经联系支持团队了解主题,但他们只建议使用Post Types Order WP plugin,它只允许您在整个网站上更改显示的"Cause",而不是允许您在逐页/后期指定它 - 基础 . 我还搜索了Google / StackOverflow并在WP Codex中尝试了各种查询,构建了一个自定义短代码(自定义VC元素本身就是一个自定义短代码: [stm_causes] ),但它只显示了最新的"Cause."

Edit 8/7/18:

我已经对Savior主题中的cause.php模板进行了几次编辑,但由于某种原因,更新的 WP_Query 循环最终没有拉入任何数据(截图3:https://i.stack.imgur.com/JLibO.png) .

唯一的例外是我在VC编辑器后端省略了任何ID;如果我没有输入ID,则默认为最近的原因 . 但是,如果我输入任何ID,即使它与最新帖子的ID相同,也不显示任何ID ...

知道我的逻辑有什么问题吗?

谢谢!

1 回答

  • 0

    render 功能已修复!

    $fea_cause = $atts['id'];    
    $donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => array($fea_cause )));
    

    为了他的帮助,Nilesh Sanura的巨大道具!

相关问题