PHP Wordpress code echo's slug not term? - php

Does anybody know how I can change the below code for my custom taxonomy (created in the Magic Fields 2 plugin) to solve my problem. I would like it to echo the name of the selected value rather than slug format. e.g. I'd like to echo Client 1 and Client 2 instead of client-1 and client-2 as it currently does.
I'd like to display multiword names with spaces and correct capitalisation e.g. Joe Bloggs Associates not joe-bloggs-associates.
project_statistics_client is the name of the field created in Magic Fields. The Type of the custom field is Term dropdown and populates with the values from my custom Taxonomy called Clients. This field is located inside of a field group that's named project_statistics but I am not sure if the group name influences the code or not?
Also please note that all the above is in a custom post type called Projects.
I have checked plugin help but still not sure:
Here is the code:
<?php $clients = get_field('project_statistics_client');
foreach($clients as $client){
echo '<div>' . $client . '</div>';
} ?>

global $post;
$taxonomy = 'your taxonomy';
$terms = get_the_terms($post->ID, $taxonomy);
if ( is_array($terms) ) {
echo(implode(',', wp_list_pluck($terms, 'name')));
}
Magic Fields 2 only provides a nice GUI to the basic WordPress taxomony feature and therefore all the WordPress taxonomy functions can be used. In particular, get_the_terms() may be used to get the categories of a post. This returns an array of objects (a post may be in multiple categories). Also, you need to extract the 'name' field from the objects.

Okay, so after a little while I remembered that I had pasted some code from a forum post which has allowed me to populate the Term dropdown field with taxonomy/category options. I now think that this is where the problem lies and why it's outputting slug and not name? I have also decided to use categories rather than taxonomy because I would like to filter the loop of posts based on the selected values and from what I read this is easier to achieve with categories. I attach below the code I have added to functions.php because a) others may want to use it with Magic Fields but also b) in the hope somebody might know what I need to change in order to output name instead of slug?
/**
* Custom Taxonomy Dropdown
*/
// initialisation
global $mf_domain;
// class with static properties encapsulating functions for the field type
class term_field extends mf_custom_fields {
public $allow_multiple = TRUE;
public $has_properties = TRUE;
public function _update_description(){
global $mf_domain;
$this->description = __("This field allows to do relations with taxonomie terms",$mf_domain);
}
public function _options(){
global $mf_domain;
// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}
$data = array(
'option' => array(
'term' => array(
'type' => 'select',
'id' => 'term',
'label' => __('related taxonomy: ',$mf_domain),
'name' => 'mf_field[option][term]',
'default' => '',
'options' => $select,
'add_empty' => false,
'description' => '',
'value' => '',
'div_class' => '',
'class' => ''
),
)
);
return $data;
}
public function display_field( $field, $group_index = 1, $field_index = 1 ) {
global $mf_domain;
// If is not required this field be added a None value
$notype = "";
if( !$field['required_field'] ) {
$notype = ( !empty($field['options']['notype']) ) ? $field['options']['notype'] : __( "-- None --" , $mf_domain );
}
$output = '';
// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}
$option_from_term_array = $field['options']['term'];
$options = get_terms($select[$option_from_term_array], array('hide_empty' => false));
$output = '<div class="mf-dropdown-box">';
$value = $field['input_value'];
$output .= sprintf('<select class="dropdown_mf" id="%s" name="%s" >',$field['input_id'],$field['input_name']);
if( $notype != "" ) {
$output .= "<option value=''>$notype</option>";
}
foreach($options as $option) {
$check = ($option->slug == $value) ? 'selected="selected"' : '';
$output .= sprintf('<option value="%s" %s >%s</option>',
esc_attr($option->slug),
$check,
esc_attr($option->name)
);
}
$output .= '</select>';
$output .= '</div>';
return $output;
}
}

Related

How to set up correctly avatar custom filter for users.php?

I'm trying to set up custom filter for avatars in users.php in administration of WordPress. I checked if user has gravatar or uploaded own picture. I'm writing this information in to the separate column of users and also record it in to the database as meta_key = 'avatar' with value Avatar or Gravatar.
I tried to implement it like is explained here, but without success.
This is my function to show filter input with button at the top of the list of users.
function filter_by_avatar($which)
{
// template for filtering
$st = '<select name="avatar_%s" style="float:none;margin-left:10px;">
<option value="">%s</option>%s</select>';
// generate options
$options = '<option value="avatar">Avatar</option>
<option value="gravatar">Gravatar</option>';
// combine template and options
$select = sprintf( $st, $which, __( 'Type of avatar...' ), $options );
// output <select> and submit button
echo $select;
submit_button(__( 'Filter' ), null, $which, false);
}
add_action('restrict_manage_users', 'filter_by_avatar');
And this is filter function:
function filter_users_by_avatar_section($query)
{
global $pagenow;
if (is_admin() && 'users.php' == $pagenow) {
// put the filtering code in here
//$top = $_GET['avatar_top'];
$top = $_GET['avatar_avatar'];
//$bottom = $_GET['avatar_bottom'];
$bottom = $_GET['avatar_gravatar'];
if (!empty($top) OR !empty($bottom))
{
$section = !empty($top) ? $top : $bottom;
$meta_query = array (array (
'key' => 'avatar',
'value' => $section,
'compare' => 'LIKE'
));
$query->set('meta_key', 'avatar');
$query->set('meta_query', $meta_query);
}
}
}
When I choose from dropdown Avatar of Gravatar and click Filter, result is only refreshing the page.

Wordpress shortcode php array to text in a text block

I am beginning to make a site in wordpress (so no php or html experience). I want to generate textlist from a mysql column into a text block.
I have made the php code for the shortcode below which returns an array which now displays in the textblock as "Array" instead of a list of strings.
If i just print the values they appear on the head of the page.
What are the next steps I need to do/look for. I can't find it because I probably do not know the correct search terms. My guess is something with HTML.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
foreach ( $marker_labels as $marker_label )
{
//print labels
echo $marker_label;
}
return $marker_labels;
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
I have now this code which gives me a list exactly what i want but not in the "paragraph block" in wordpress where my shortcode is situated.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
foreach ( $marker_labels as $marker_label )
{
echo '<li>'. $marker_label.'</li>';
}
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
I'm not 100% sure what you're trying to accomplish, but try this. You don't want to echo out all the values as you're looping through them. Concatenate everything in a variable and then return the entire string at the end of your shortcode. This should generate an unordered list.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
$output = '<ul>';
foreach ( $marker_labels as $marker_label )
{
$output .= '<li>' . $marker_label . '</li>';
}
$output .= '</ul>';
return $ouput;
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>

How can I get terms (post_tag) of custom taxonomy item? Wordpress

I created custom post type 'photo' and custom taxonomy 'catphoto'.
The type 'photo' supports 'catphoto' and 'post_tag' as taxonomies.
Now I should make one filter for a client.
I need to show on taxonomy-catphoto.php page all post_tags, which belong to the current 'catphoto' item.
For example, I have a post of custom post type 'photo'. The name of this post is 'Plane'. This post belongs to '1961-1981' catphoto item. Also it has post tags like 'space', 'planes', 'stars', 'war'.
Also, for example, I have a post of 'photo' which name is 'Soldier'. It belongs to '1941-1961' catphoto item and has 'WW2', 'USA', 'USSR' like post tags.
And now when I select 1941-1961 catphoto item I should get a list with:
WW2
USA
USSR
I try like this:
if (substr($_SERVER['REQUEST_URI'],1,8) == 'catphoto'){
$cur_terms = get_terms('post_tag');
if ($cur_terms) {
foreach( $cur_terms as $cur_term ){
echo $cur_term->name.'<br/>';
}
}
}
Now I get all post tags of all catphoto items. How can I fix to restrict for definite catphoto item. For example, '1941-1961' ?
I have solved this via $_GET query. So easy...
For beginning, I had decided to create own taxonomy type (like post_tags). Its name is 'mark'.
and after that the snippet is:
if (substr($_SERVER['REQUEST_URI'],1,8) == 'catphoto'){
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$query = new WP_Query( array('post_type' => 'photo', 'catphoto' => $term->name));
echo '<ul>';
$uniqueTerms = array();
$uniqueSlugs = array();
$uniquePar = $term->slug;
while ( $query->have_posts() ) {
$query->the_post();
$terms = wp_get_post_terms(get_the_ID(), 'mark', array("fields" => "all"));
foreach ($terms as $term1)
{
if(!in_array($term1->name, $uniqueTerms)) {
$uniqueTerms[] = $term1->name;
$uniqueSlugs[] = $term1->slug;
}
}
}
for ($var = 0; $var < count($uniqueTerms); $var++)
echo '<li>'.$uniqueTerms[$var].'</li>';
echo '</ul>';
}
So ugly code here and there but it works.
and now in taxonomy-catphoto.php I fill in:
$mark = $_GET['mark'];
and after that a little change inside WP_Query query
$query = new WP_Query( array( 'post_type' => 'photo', 'posts_per_page'=>56, 'paged'=> $paged, 'catphoto' => $term->name, 'mark' =>$mark) );
Best regards, Igor

How to get only child terms of current post type?

Having a custom post type 'pubs' with custom taxonomy 'types' in which admin enter parent terms and their child terms.
Using this code to get all the terms of current post type:
$object_terms = wp_get_object_terms($post->ID, 'types', array('fields' => 'all'));
if ($object_terms) {
echo '' . '' . '' ;
$res = '';
foreach ($object_terms as $term) {
$res .= $term->name . ',';
}
echo rtrim($res,' ,').'' . '';
}
this code displays both parent & child terms.
Is there any way to exclude parent terms from the result? I need the code to display only child terms related to the current post.
Untested, but I think if you put the following inside your foreach loop at the very top, you'll get only the children:
if ($term->parent == 0) continue;
Just for someone still looking:
Solution is for when you have multiple level of hierarchy and you want just last level.
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
foreach ($term_array as $term_id){
$children=get_term_children($term_id, $taxonomy);
if(empty($children)){
$exclude=$term_id;
}
}

Metabox select arrays is display post id instead of post name in the custom column

I am having a little trouble displaying a post name in a admin column on my custom page in wordpress. I have everything working but when I call the selected item that was chosen from a drop down from a custom metabox it displays the post ID in the admin column instead of the post name.
Here is the code I am using ( to display the columns)
add_filter('manage_edit-projects_columns', 'edit_projects_columns');
function edit_projects_columns($columns) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Project Title' ),
'client' => __( 'Client' ),
'protype' => __( 'Project Type' ),
'featureImage' => __( 'Project Image' ),
);
return $columns;
}
To Display the selected fields
add_action('manage_projects_posts_custom_column', 'manage_projects_columns', 10, 2);
function manage_projects_columns($column, $post_id, $selected) {
global $post;
switch($column) {
/* Display Column */
case 'client' :
/* Get the post meta. */
$selected = get_post_meta($post->ID, 'a_clients', true);
if (empty($selected))
echo __('');
else
printf(__( '%s' ), $selected);
break;
/* End Display Column */
/* If displaying the 'genre' column. */
case 'protype' :
$terms = get_the_terms($post_id, 'tagwork');
if (!empty($terms)) {
$out = array();
foreach($terms as $term) {
$out[] = sprintf('%s',
esc_url(add_query_arg(array('post_type' => $post->post_type, 'tagwork' => $term->slug), 'edit.php')),
esc_html(sanitize_term_field('tagwork', $term->name, $term->term_id, 'tagwork', 'tagwork'))
);
}
echo join(', ', $out);
} else {
_e('');
}
break;
case 'featureImage' :
$column = get_the_post_thumbnail($post->ID, 'featureImage');
if (empty($contact))
echo __('');
else
printf(__('%s'), $contact);
break;
default :
break;
}
}
The selected field I am having trouble with is client
/* Display Column */
case 'client' :
/* Get the post meta. */
$selected = get_post_meta($post->ID, 'a_clients', true);
if (empty($selected))
echo __('');
else
printf(__('%s'), $selected);
break;
It is displaying the post ID instead of the post name.How would I go about fixing this?
Extra Note: The column is pulling the client information from a drop down menu on a projects page. The menu is pulling the titles from any new client I add to the client page. This way I can assign a project to a specific client.
In your example $selected is being assigned the value of the a_clients meta value, which seems to be a post_id. You need to use this post_id to fetch the WP_Post for the client and then use the post_title property to print out the proper title. You can leave out the else since it's just an empty string.
/* Display Column */
case 'client' :
/* Get the post meta. */
$client_id = get_post_meta( $post->ID, 'a_clients', true );
// Will be true if a_clients is not "" or 0
if ( !empty($client_id) ){
// Get the post for the client_id
$client = get_post($client_id);
// If we have a title, print it out, else just use the ID
printf( __( '%s' ), !empty($client->post_title)? $client->post_title : $client_id);
}
break;

Categories