wp post metadata showing in backend but not frontend / same code - wordpress

Situation:
using a php file (table_template.php) with include_once to show meta data. Using this template in the backend (inside a metabox) shows everything as it should - using the same template on the frontend (inside a post) the table metadata do not show (but other meta data from custom fields do).
var dump in the backend:
string(65) "[["test value 1","test value 2","test value 3"],["test value a","test value b","test value c"]]"
var dump on the frontend:
string(6) "[[""]]"
code from table_template.php:
<?php
/* TEMPLATE TO RENDER THE TABLE (AS PREVIEW) IN THE BACKEND AND FRONTEND */
global $post;
$table_meta = get_post_meta( $post->ID, 'psg_table_meta', true ) ?
get_post_meta( $post->ID, 'psg_table_meta', true ) : '[[""]]';
$t = json_decode( $table_meta );
$c_id = get_post_meta( $post->ID );
$c = get_post( $c_id );
?>
<div class="psg_box_table">
<table class="psg_table ">
<thead>
<tr>
<?php foreach ( $t[ 0 ] as $col ): ?>
<th>
<?php echo $col; ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ( $t as $idx => $row ): ?>
<?php if ( $idx == 0 )
continue; ?>
<tr>
<?php foreach ( $row as $col ): ?>
<td>
<div class="psg_table_content">
<?php echo str_replace( '"', '"', $col ) ?>
</div>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!--END SIZING CHART TABLE -->
How i load the Template:
include_once( "table_template.php" );

If you are using the template inside loop of WP_Query, then replace
$post->ID
with
get_the_ID();
Also check the psg_table_meta key is correct one.

The issue was, that i should not check for the postID (the table meta data is not directly in the post, but in a selected/linked post within it. So i had to replace
$table_meta = get_post_meta( $post->ID, 'psg_table_meta', true ) ?
with
$table_meta = get_post_meta( $psg_selected, 'psg_table_meta', true ) ?

Related

Print serialized data from phpmyadmin saved using cf7db

I want to build a custom php page that shown registered user from wp,the data saved from wp is a serialized data and i got problem to display. btw i am using contact form 7 database plugin.
i keep getting explode() expect parameter to be string and getting error on line 10.
<?php
//connect database
$conn=new mysqli("localhost","root","","testsaja2");
//call a field from table
$sql= "select form_value from wp_db7_forms";
$result=mysqli_query($conn,$sql);
while($string=mysqli_fetch_array($result));
$explode = explode(PHP_EOL, $string);
foreach ($explode as $line) {
?>
<?php
foreach (unserialize($line) as $item => $value){
?>
<tr>
<td><b><?php echo $item . ": ";?></b></td>
<td><?php if ($value == 'email'){
foreach ($value as $data) {
echo $data ;
}
}
else {
echo $value;
}
?> </td>
</tr>
<br>
<?php } ?>
</table>
<?php } ?>
Modify your code to this.
<?php
//connect database
$conn=new mysqli("localhost","root","","testsaja2");
//call a field from table
$sql= "select form_value from wp_db7_forms";
$result=mysqli_query($conn,$sql);
while($string=mysqli_fetch_array($result)){
$unserialize = unserialize($string['form_value']);
foreach ($unserialize as $item => $value){
?>
<tr>
<td><b><?php echo $item . ": ";?></b></td>
<td><?php echo $value; ?> </td>
</tr>
<?php
} }?>
</table>

Wordpress ACF Repeater Field - Only pull in first 2 fields

Topic kinda says it all. I need to only pull in the first two fields of an ACF repeater field.
Here is what I am trying to use but it is obviously not right:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
How am I supposed to do this?
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
You are mixing apples and pears, WP_Query and ACF Repeater field. WP_Query returns the post data, whilst the ACF function have_rows( $repeater_field_name, $post_id ); checks whether there are any data in the custom field repeater that is on your page/post (or if you specify $post_id on the relevant post/page). More info at https://www.advancedcustomfields.com/resources/repeater/
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>

WooCommerce get variable product attribute label

I'm trying to display the details from my custom product attributes on a table on my single product page. I can get the product variation SKU, but I can't seem to get the "label" (the best I can do is get the slug).
This is the code that I'm currently working with:
<!-- ******* START PRODUCT VARIABLE TABLE SECTION ******* -->
<?php
global $woocommerce, $product, $post;
if( $product->is_type( 'variable' ) ) :
$available_variations = $product->get_available_variations();
// START CHECK IF VARIATIONS ARE PRESENT
if ( $available_variations ) :
$attributes = $product->get_attributes();
?>
<div class="row">
<div class="small-12 column">
<table class="varations-table">
<thead>
<tr>
<th class="small-4">ORDER #</th>
<th class="small-5">DESCRIPTION</th>
<th class="small-3">UOM</th>
</tr>
</thead>
<tbody>
<?php foreach ($available_variations as $prod_variation) : ?>
<?php
// get some vars to work with
$post_id = $prod_variation['variation_id'];
$post_object = get_post($post_id);
?>
<tr>
<td><?php echo $prod_variation['sku']; ?></td>
<?php foreach ($prod_variation['attributes'] as $attr_name => $attr_value) : ?>
<td><?php echo $attr_value; ?></td>
<?php endforeach;?>
<td class="uom"><?php echo the_field( 'uom' ); ?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
</div>
<?php
// END CHECK IF VARIATIONS ARE PRESENT
endif;
endif;
?>
<!-- ******* END PRODUCT VARIABLE TABLE SECTION ******* -->
Does anyone know how I can pull the Variable label (not just the slug, replacing dashes with spaces using str_replace() )?
If you have the slug, you can get a WordPress taxonomy object with the get_taxonomy() function. So to get the label of a WooCommerce custom attribute you just have to do get_taxonomy($attribute_slug)->labels->name.
<?php if ( $product->is_type( 'variable' )) : ?>
<div class="size">
<span class="name-size">SIZE</span>
<span class="value-size">
<?php
$attr = get_post_meta($post->ID, '_product_attributes', true);
echo $attr['size']['value'];
?>
</span>
</div>
<?php endif; ?>

WP event manager widget only show if there is some events

i want to show event title if there is some events, if no event is there i dont want to show event title. how can i solve this problem, please help me. iam using Event Manager plugin.sorry for my bad english
Step 1 : Inside your WP site there is the following path: \site.com\wp-content\themes\theme_name\functions.php
*In case of not having this file you created.
Step 2 : Please insert into file functions.php the following code:
<?php
function em_mod_custom_events_list(){
$events = EM_Events::get(array('scope'=>'future','limit'=>10));
?>
<table>
<tbody>
<?php
$start = false;
$limit = 3;
$count = 1;
foreach( $events as $EM_Event ){
if ( !empty($EM_Event->event_name) ){
?>
<?php if ( !$start ) { ?>
<tr>
<?php $start = true; ?>
<?php } ?>
<td><?php echo $EM_Event->output("#_EVENTLINK"); ?></td>
<?php
if ( $count == $limit ){
?>
</tr>
<?php
$start = false;
$count = 1;
}else{
$count++;
}
?>
<?php
}
}
?>
</tbody>
</table>
<?php
}
Step 3 : Save file and Test
1) Using templates --> http://wp-events-plugin.com/documentation/using-template-files
2) Create your own page event --> http://wp-events-plugin.com/tutorials/create-custom-event-information-pages/
The documentation is quite clear and you can help.

How to display the_meta() of a Wordpress custom page in a table

I have some meta values on my wordpress custom page that I need to be displayed in a table. Is there any way I can do that.
Here's the code I'm using now: <?php the_meta(); ?>
And this is what it shows:
I want to do something like:
I found out that:
the_meta() is located in wp-includes/post-template.php
This makes the output:
<ul class='post-meta'>
<li><span class='post-meta-key'>your_key:</span> your_value</li>
</ul>
So it's not recommended to edit files in that folder because of the wordpress update.
<table>
<tr><td colspan="2">Game Summary</td></tr>
<?php
$meta = get_post_meta( get_the_ID() );
$exclude = array('_edit_last', '_wp_page_template', '_edit_lock');
foreach( $meta as $key => $value ) {
if( in_array( $key, $exclude) )
continue;
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value[0]; ?></td>
</tr>
<?php
}
?>
</table>

Resources