• Creator
    Topic
  • #11595
    Resolved dhuy
    Participant

    Hi,
    I've created a CPT for pricing tables and I want to show it in a page using the post field.
    Currently, I'm stuck at how to get the CPT id to display the pricing table.

    $table = isset( $pricing['pricing_table_id'] ) ? $pricing['pricing_table_id'] : '';
                      if( $table ) {
                        setup_postdata( $GLOBALS['post'] =& $table );
                        $plans = rwmb_meta( 'plan' );
                        if( ! empty( $plans ) ) {
                          foreach ($plans as $plan) {
                            $name = isset( $plan['plan_name'] ) ? $plan['plan_name'] : '';
                            echo $name;
                          }
                        }
                        wp_reset_postdata();
                      }
Viewing 3 replies - 11 through 13 (of 13 total)
  • Author
    Replies
  • #11670
    dhuy
    Participant

    I put all the code in a function outside the main function, then call it, yet nothing happens.

    // Sub function
    function pt() {
      $table = isset( $pricing['pricing_table_id'] ) ? $pricing['pricing_table_id'] : '';
      if ( $table ) { 
      $plans = rwmb_meta( 'plan', '', $table );
        if ( ! empty( $plans ) ) {
          foreach ($plans as $plan) {
            $name = isset( $plan['plan_name'] ) ? $plan['plan_name'] : '';
            echo $name;
          }
        }
      }
    }
    
    //Main function
    function page_section() {
      $sections = rwmb_meta( 'section' );
      if( ! empty( $sections ) ) {
        foreach ( $sections as $section ) {
          if($section == 'pricing') { pt() }
        }
      }
    }
    #11685
    Anh Tran
    Keymaster

    Hi Huy,

    Your code has a PHP error and can't work. The function pt doesn't understand the variable $pricing.

    I have rewritten your code as follows, please try it:

    // Sub function
    function pt( $pricing ) {
      $table = isset( $pricing['pricing_table_id'] ) ? $pricing['pricing_table_id'] : '';
      if ( ! $table ) {
        return;
      }
      $plans = rwmb_meta( 'plan', '', $table );
      if ( empty( $plans ) ) {
        return;
      }
      foreach ($plans as $plan) {
        $name = isset( $plan['plan_name'] ) ? $plan['plan_name'] : '';
        echo $name;
      }
    }
    
    //Main function
    function page_section() {
      $sections = rwmb_meta( 'section' );
      if ( ! empty( $sections ) ) {
        return;
      }
      foreach ( $sections as $section ) {
        if ( empty( $section['section_pricing'] ) {
          continue;
        }
        $pricing = $section['section_pricing'];
        pt( $pricing );
      }
    }
    #11750
    dhuy
    Participant

    Everything is OK now.
    Smooth migration from ACF.

Viewing 3 replies - 11 through 13 (of 13 total)
  • You must be logged in to reply to this topic.