• Resolved bobjgarrett

    (@bobjgarrett)


    I need to get a custom field into PHP as part of an event-list page so I can interrogate it in an if/then.
    I can display it using #_ATT{Race} but getting it into a PHP variable is eluding me.
    I thought it should be get_post_meta($EM_Event->output, “Race”, true) but this does not work.
    What am I doing wrong?

Viewing 15 replies - 1 through 15 (of 17 total)
  • jordanthedesigner

    (@jordanthedesigner)

    Perhaps this is what you are looking for:

    <?php echo $EM_Event->output('#_ATT'); ?>

    Thread Starter bobjgarrett

    (@bobjgarrett)

    Thanks for prompt feedback but this
    echo $EM_Event->output(‘#_ATT{Race}’);
    seems to break the page.
    Is this because I am using the event_list shortcode which is not opening the data as $EM_Event or is the syntax still wrong?

    Plugin Support angelo_nwl

    (@angelo_nwl)

    if you are using wp do_shortcode with events_list shortcode then it might be the reason and maybe you can try this snippet

    
    $EM_Events = EM_Events::get( array('scope' => 'future' ) );
    foreach ( $EM_Events as $EM_Event){
    
     $location = $EM_Event->get_location();
     $category = $EM_Event->get_categories()
    
    }
    
    Thread Starter bobjgarrett

    (@bobjgarrett)

    I am using
    [events_list]

    #_EVENTDATES
    #_EVENTNAME
    etc
    [/events_list]
    does this not set up data I can use?

    Plugin Support angelo_nwl

    (@angelo_nwl)

    using shortcode events_list, you can use #_ATT{fieldname}

    Thread Starter bobjgarrett

    (@bobjgarrett)

    Yes, but to go back to my original question, how do I get that value into a PHP variable?

    caimin_nwl

    (@caimin_nwl)

    Try:

    echo $EM_Event->output('#_ATT{fieldname}');

    Thread Starter bobjgarrett

    (@bobjgarrett)

    That seems to break the page as even the fields such as #_EVENTDATES and #_EVENTNAME do not appear but instead the shortcodes are reproduced.

    Can you post the complete code snippet you’re using?

    Thread Starter bobjgarrett

    (@bobjgarrett)

    See below. The critical part is where the variables $filetofind1 etc. need to include the value of #_ATT{Race}

    [events_list year=”<?php echo $year; ?>”]
    <div style=”float:left; clear:both; min-height:32px; border-top: 1px solid rgb(37, 129, 170); width:100%;”>
    <div style=”float:left; width:210px;”>
    <div style=”float:left; width:30px; text-align:left; padding-right:10px;”>
    #_ATT{Race} </div>
    <div style=”float:left; width:170px; clear:none;”>
    #_EVENTDATES  #_24HSTARTTIME</div>
    </div>
    <div style=”float:left; clear:none;”>
    #_EVENTNAME
    {is_future}<i>#_ATTENDEESPENDINGLIST</i>{/is_future}
    </div>
    <?php
    // $filetofind1 = “wp-content/files/event-reports/”. $year . “-“. . “.pdf”;
    // $filetofind2 = “wp-content/files/event-results/”. $year . “-“. “.pdf”;
    // $filetofind3 = “wp-content/files/event-instructions/”. $year . “-” . “.pdf”;
    echo “Race No. as variable” . $EM_Event->output(‘#_ATT{Race}’);
    if (file_exists($filetofind1) || file_exists($filetofind2) || file_exists($filetofind3)) {
    ?>
    <div style=”float:right; width:200px;”>
    <div style=”float:right; width:50px; clear:none; text-align:right;”>
    <?php
    if (file_exists($filetofind1)){ ?>” target=”_blank”>Event Report
    <?php };
    ?>
    </div>
    <div style=”float:right; width:50px; clear:none; text-align:right;”>
    <?php
    if (file_exists($filetofind2)){ ?>” target=”_blank”>Event Results
    <?php } else {echo ” “;};?>
    </div>
    <div style=”float:right; width:50px; clear:none; text-align:right;”>
    <?php
    if (file_exists($filetofind3)){ ?>” target=”_blank”>Event Instructions
    <?php } else {echo ” “;};?>
    </div>
    </div>
    <?php }?>
    <?php
    ?>

    [/events_list]

    Plugin Support angelo_nwl

    (@angelo_nwl)

    just to confirm, are you using wp do_shortcode with php with the above code?

    Thread Starter bobjgarrett

    (@bobjgarrett)

    No, I am not. Should I?
    I am not sure how this might solve the problem though.
    All the shortcodes are showing up it is just trying to get a value into PHP that seems to be a problem.

    • This reply was modified 6 years, 12 months ago by bobjgarrett. Reason: extra info

    Yes, you will need to use something like do_shortcode. Where are you using that code?

    If you’re using it within the admin settings the PHP code won’t run. If you’re using within a PHP file, the shortcodes and placeholders won’t run.

    Thread Starter bobjgarrett

    (@bobjgarrett)

    Thanks for that. I was not aware of do_shortcode and still don’t understand it but here is a simplified version which works to get the value into a php variable for anyone else that needs something similar.
    <?php
    global $wpdb;
    ?>
    [events_list year=”2017″]
    Race No. #_ATT{Race}
    #_EVENTDATES
    #_EVENTNAME
    <?php
    echo “Race No. as variable=”;
    echo do_shortcode(‘#_ATT{Race}’);
    ?>
    <hr>
    [/events_list]

    Thread Starter bobjgarrett

    (@bobjgarrett)

    Sorry to pursue this further but I thought it was resolved, but it is not.
    If we take the example above but have a statement $raceno=do_shortcode(‘#_ATT{Race}’} then echo $raceno then the output is for example “17”. Yet if I compare this variable with “17” it is not equal! If I do a strlen($raceno) then it is 11!
    So we have a variable which if displayed is 17 but if compared is the string #_ATT{Race}.
    How does that work! And how can I actually get a php variable with the actual value so I can do compares etc.
    Thanks again

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Get Custom Fields into PHP’ is closed to new replies.