I'm using Wordpress custom post field for my products in Woocommerce.
I've created a custom field called 'phone_model' and it will contain strings like 'iPhone 6 Plus' etc.
In my child theme functions.php I've added this function:
function wc_phone_model(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
foreach ( $phone_model as $key => $value ) {
echo $value;
}
}
}
It's working as expected. It will show the given value in the custom post field.
In my Woocommerce title loop title.php, I've added this line:
<div class="phone_model_wrap"><span class="phone_model"><?php wc_phone_model() ?></span></div>
How do I make this show only if there is something to show from the custom post field?
Thanks in advance guys!
Cheers Kenn
I know there is a stigma against echoing html, but in this case, I can't think of a better way. I'd recommend something like this:
function wc_phone_model(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
echo '<div class="phone_model_wrap"><span class="phone_model">';
foreach ( $phone_model as $key => $value ) {
echo $value;
}
echo '</span></div>';
}
}
Then in your title.php, its just a matter of calling the function.
<?php wc_phone_model() ?>
If someone has a clean solution without echoing html though, I'd be interested in seeing it!
#kunruh
I have faced another problem with your solution.
I have made a new function to be used elsewhere with another class etc.
function wc_phone_model_single_product(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
echo '<div class="phone_model_wrap"><span class="phone_model_single_product">Til ';
foreach ( $phone_model as $key => $value ) {
echo $value;
}
echo '</span></div>';
}
}
The "Til" will be echoed even if there is nothing in $phone_model.
How do I avoid this?
Cheers Kenn
Related
I want to add text "Genre:" to the beginning of this custom field
<?php the_field('im_genre'); ?>
the reason that i dont simply add something like this
<li>Genre:<?php the_field('im_genre'); ?> </li>
is that i dont want to this text be shown when there isnt this field for a post
You can check if the field exists and then display your HTML element.
<?php if( get_field('im_genre') ): ?>
<li>Genre:<?php the_field('im_genre'); ?> </li>
<?php endif; ?>
I believe you can do it by using the "acf/format_value" hook.
add_filter('acf/format_value', 'reFormatValue', 20, 3);
function reFormatValue($value, $post_id, $field) {
$data = [
// list of field keys to format
'field_XXXXXXX' => 'XXX:',
'im_genre' => 'Genre:',
// etc...
];
$keys = array_keys($data);
if (in_array($field['key'], $keys)) {
$value = $keys[$field['key']] . $value;
}
return $value;
}
add this to your functions.php (active theme folder) or any active plugins
I'm a PHP beginner working with Wordpress, and I'm trying to retrieve data from custom fields (using Custom Field Suite) from a post to be displayed on a different page.
I have tried a few different ways, and found two methods that work. But as I'm a beginner, I wonder if these methods are 'proper'?
This is one solution I found, but it's not exactly elegant:
// Method 1
$current = CFS()->get( 'get_current' ); //retrieves the post ID from a custom field on the page
$custom_fields = get_post_custom($current);
$my_custom_field = $custom_fields['current_title'];
$my_custom_field2 = $custom_fields['current_artist'];
foreach ( $my_custom_field as $key) {
}
foreach ( $my_custom_field2 as $key2) {
}
echo '<h2>'.$key.'</h2>';
echo '<h1>'.$key2.'</h1>';
I tried to re-write it like this, but nothing is displayed - not sure what's wrong with this loop:
// Method 2
$current = CFS()->get( 'get_current' );
$custom_fields = get_post_custom($current);
foreach ( $custom_fields as $key) {
echo '<h2>'.$key['current_title'].'</h2>';
echo '<h1>'.$key['current_artist'].'</h1>';
}
As method 2 wasn't working, I tried something else and found that this also works (I added the loop here based on this answer: https://stackoverflow.com/a/19918170/5483154):
// Method 3
<?php while ( have_posts() ) : the_post();
$current = CFS()->get( 'get_current' );
$currentitle = get_post_meta($current, 'current_title', true);
$artistname = get_post_meta($current, 'current_artist', true);
echo '<h2>'.$currentitle.'</h2>';
echo '<h1>'.$artistname.'</h1>';
endwhile;
wp_reset_query();
?>
Method 3 seems best to me. Is this a good way of approaching this problem? And if anyone would be kind enough to explain what's wrong with method 2, I would also appreciate it. Thanks
Looking at the Custom Field Suite documentation here: http://customfieldsuite.com/api/get.html
It looks like you should be using the 'get' method:
CFS()->get( $field_name, $post_id, $options );
Eg.
$current = CFS()->get( 'get_current' );
echo '<h2>' . CFS()->get( 'current_title', $current ) . '</h2>';
echo '<h1>' . CFS()->get( 'current_artist', $current ) . '</h1>';
Ps. Don't forget to watch the order of your heading tags (<h1> should come before <h2>)
In the case of your "method 2": what if you try rewriting it something like this?
// Method 2
$current = CFS()->get('get_current');
$custom_fields = get_post_custom($current);
foreach ($custom_fields as $field) {
foreach ($field as $key => $value) {
echo $key . ': ' . $value;
}
}
I am trying to list all #titles in a Drupal form module; however, I can't seem to get it to work.
foreach ($form_values as $key => $value) {
echo $form['myForm'][$key]['#title'];
}
The method above does not return anything. I can list the $key values by doing the following:
foreach (...) {
echo $key . ' ';
}
but I can't find a way to list the titles (e.g., '#title' => t('Name')) for each form input.
Any help?
Thanks!
Your request a bit baffles me, but
function print_title($form) {
foreach (element_children($form) as $key) {
print_title($form[$key]);
}
if (isset($form['#title'])) {
// do something with your title
}
}
I am making a photography website. In the DB there is an images table, events table and category table which as linked via foreign keys. At the moment i generate events from when a photo was taken from the database and turn them into anchor links.
<?php
while ( $a_row = mysql_fetch_row( $result ) ){
foreach ( $a_row as $field ){
?>
<php? echo $field; ?>
<?php
}
}
?>
When the link is clicked a script gets the variable from get in the url: /images.php?**event**=eventCalledParty
foreach($_GET as $value)
{
$event = $value;
}
$event = mysql_real_escape_string($event);
My question is - If i was to implement categories and have a url that looks like:
/images.php?event=eventCalledParty&category=categoryCalledFashionPhotography
How would i seperate these two variables from the url to use in my queries.
Thank You.
These will automatically show up in these variables...
$_GET['event']
$_GET['category']
PHP does all of this work for you.
$event = mysql_real_escape_string($_GET['event']);
$category = mysql_real_escape_string($_GET['category']);
Each url parameter becomes a separate key in $_GET.
So, for /images.php?event=eventCalledParty&category=categoryCalledFashionPhotography you will get:
$_GET['event'] => 'eventCalledParty'
$_GET['category'] => 'categoryCalledFashionPhotography'
and you can act accordingly:
if ($_GET['category'] == 'categoryCalledFashionPhotography') {
//do stuff here
}
Im a bit rusty on php but I think in your foreach what you want is to get the name of teh field as well as its value.
foreach($_GET as $name->$value)
{
if($name == 'event') {
$event = mysql_real_escape_string($value);
}
else if($name == 'category')
{
$category = mysql_real_escape_string($category);
}
}
foreach($_GET as $key => $value)
{
$event[$key] = mysql_real_escape_string($value);
}
This worked perfectly in a previous version of wordpress, different site tho. I have a custom field called flash on several wp pages but it keeps defaulting to "printed if results are empty". Any ideas on why this wouldn't work?
<?php
if ( function_exists('get_post_custom_values') )
{
$mykey_values = get_post_custom_values('flash');
if(count($mykey_values) > 0)
{
foreach ( $mykey_values as $key => $value )
{
echo "$value";
}
}
else
{
//printed if results are empty
echo("mainPage.swf");
}
}
else
{
//printed if function doesn't exist
echo("mainPage.swf");
}
?>
Hi not really sure why that is not working (as long as you are sure you do have custom fields with 'flash' values in the post). Anyhow, try the following, I am sure the following will work.
<?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['flash'];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>