ElseIf Custom Field different value (Wordpress) - wordpress

(sorry for my English) and I hope to explain!
I'm looking for a solution to display different output for a custom filed, named "cw". The my theme of much use in custom filed, so I created a function to simplify your calls:
/**
* Get custom field of the current page
* $type = string|int
*/
function my_getcustomfield($filedname, $page_current_id = NULL)
{
if($page_current_id==NULL)
$page_current_id = get_page_id();
$value = get_post_meta($page_current_id, $filedname, true);
return $value;
}
said this, I created the custom filed in my theme:
<?php $video_code = my_getcustomfield('cw',get_the_ID()); if(!empty($video_code)) : ?>
<div class="video_code"><?php echo $video_code; ?></div>
Since the custom value is a URL, type: http://127.0.0.1:4001/?f=cadbe2676d5108523f931a68eedb3582, I need to extract just the ID. using this technique PHP Regex to get youtube video ID?, I modified my custom filed in this way:
<?php $video_code = my_getcustomfield('cw',get_the_ID()); $url = $video_code; parse_str(parse_url($url, PHP_URL_QUERY )); if(isset($video_code[0])){ ?>
<div cacaolink="http://127.0.0.1:4001/?f=<?php echo $f; ?>"></div>
<?php } else { ?>
<?php echo 'Video n.d.'; ?></div>
<?php endif; } ?>
In this way everything works perfectly.
Now, the custom value can be those listed below:
<div cacaolink="http://127.0.0.1:4001/megavideo/megavideo.caml?videoid="></div>
<div cacaolink="http://127.0.0.1:4001/videobb/videobb.caml?videoid="></div>
<div cacaolink="http://127.0.0.1:4001/?f="></div>
**Finally here is the problem: How can I use an elseif according to the custom value?
any help is appreciated :)**

So what you need in short is extracting "video id" part from urls having one of 3 structures defined above. If I understood you right, you can just use the next code:
$video_id = preg_replace("/.*\?(videoid|f)=/", '', $customvalue);
Where $customvalue is the value extracted from your custom field with my_getcustomfield.

Related

Assign an image to a taxonomy in wordpress?

I am trying to get a different icon to appear next to an event in Wordpress. I am use the "The Event Calendar" plugin and the events are stored as taxonomies. I have three taxonomies. "Tax1", "Tax2" and "Tax3".
I cannot seem to figure out how to call to check what the taxonomies are and assign an icon to each one. Here is what I have so far.
" title="" rel="bookmark">
<?php $taxonomy_exist = taxonomy_exists( 'Tax2' ); ?>
<?php if(is_tax($taxonomy_exist)) { ?>
<p>working!!!!!!!!!!</p>
<?php } else { ?>
<img class="cat-icon" src="/wp-content/uploads/2017/11/Heart.png" />
<?php } ?>
<?php $title = get_the_title($event); ?>
<?php if (strlen($title) > 30) { ?>
<?php echo mb_strimwidth($title, 0, 30, '...'); ?>
<?php } else { ?>
<?php echo $title ?>
<?php }?></a></span><!--.event-title-->
So it is getting to the if statement and failing, showing the heart icon. How can I see if it is part of the "Tax2" taxonomy?
Problem 1:
is_tax() accepts taxonomy names as a first argument. But you put $taxonomy_exist parameter which is boolean. (true/false). Of course it should not work and always return zero. Because there is not any taxonomy called "true" or "false".
Problem 2: Are you sure that tax1, tax2 and tax3 are taxnomies? may be they are terms? if they are terms then i will add a solution code as an update to this answer. If they are taxonomies, then i don't see any logic there. Because taxonomies are not objects to be related to posts. Taxonomy is name of categorization, terms are member elements of those taxonomies.

wordpress add field to post_class

I need to add a custom field to my article post but not sure how to add an additional class to it.
Currently the classes get pulled through like this <?php post_class($classes); ?>.
However I need to add a custom field to this as well. To demonstrate ive added a class= but this doesnt work as class= is being added twice.
<?php post_class($classes); ?> class="<?php the_field( "size" ); ?>
So i need post_class and the_field to work together.
Thanks for your answers but I've found a simple way to do this
<?php post_class(get_field('field_name')); ?>
You can do this with two different way,
First:- Add following code in theme's functions.php file:
This will add your class where post_class is called.
function category_id_class($classes)
{
global $post;
if($post->post_type == 'post')
{
$classes[] = get_field( "size" );;
}
return $classes;
}
add_filter('post_class', 'category_id_class');
Second:- Add this following code directly into your page:-
$post_classes = get_post_class();
$post_classes= implode(' ', $post_classes);
echo 'class="'.$post_classes. the_field( "size" )'"';
Hope this will help you little bit.
So why you could not do like this:
<?php post_class(the_field( "size" )); ?>
Because it is working like this:
<?php post_class('my_custom_class'); ?>

Custom Fields/Meta Boxes - Only Display If Value is Present

I am using the Custom Metaboxes and Fields for WordPress add-on to create custom fields for custom post types. However, I need a way to display content only if a value exists for a specific custom field.
Currently, I am using this code:
<?php
$url = get_post_meta($post->ID, 'snippet-reference-URL', true);
if ($url) {
echo "<p><a href='$url'>Reference URL</a></p>";
} ?>
However, this displays content if the field is present (which in this case, is always). I need code that will only display content if a specific field has a value.
check this example given on codex page
<?php
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
?>

Wordpress - Add comma to all but last item, remove underscore

I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>

Wordpress Custom Value If/ElseIf - Doesn't Work

I'm outside the loop and want to call a custom value "featvideo" and display it. If there isn't "featvideo" then print an image...
The video displays, but when there isn't a video a blank box displays. You can see the the issue here: http://wgl.buildthesis.com
(and yes, $images is a function defined in functions.php and works)
<?php
$feat_catbox_1 = new WP_Query("cat=$tt_feat_id&showposts=$tt_feat_postcount");
while ($feat_catbox_1->have_posts()) : $feat_catbox_1->the_post();
$key = 'featvideo';
$video_url = get_post_custom_values($key);
$featuredvideo = $video_url[0];
?>
<div class="contentdiv">
<div id="featured-thumb">
<?php if ($key=="featvideo")
echo $featuredvideo;
elseif ($key=="")
echo $images('1', '390', '244', 'alignleft', true); ?>
</div>
</div>
Since you have set $key = 'featvideo' and then test if it is set later, it will always return true. You never change the value of $key anywhere in your code except when you set it.
I would suggest something like the following for your if statement:
<?php
if($featuredvideo)
echo $featuredvideo;
else
echo $images('1', '390', '244', 'alignleft', true);
?>

Resources