I am trying to output this custom meta in one of my WordPress page templates. The documentation of the plugin seems to be lacking. ( http://metabox.io/docs/get-meta-value/ )
Because that I have clone as true it is displayed as so in the custom post type
I am trrying to display it VIA html so maybe the output would be something like
<ul>
<li>Red LED footwell lighting</li>
<li>Red LED Trunk Lighting</li>
<li>etc...</li>
</ul>
Here is how I defined the item I am trying to display
array(
'name' => 'Interior Mods',
'desc' => 'List all of the interior mods',
'id' => $prefix . 'intmods',
'type' => 'text',
'std' => '',
'class' => 'custom-class',
'clone' => true,
),
Thanks
You can use plug-in codes for get value.
$values = rwmb_meta(
'YOUR_PREFIX_text',
$args = array(
'type'=>'text',
// other options for meta field
),
$post_id = $post->ID
);
if($values){
echo "<ul>";
foreach ($values as $key => $value) {
echo "<li>".$value."</li>";
}
echo "</ul>";
}
Related
I have created an elementor SELECT2 Control and now I want to display selected categories post Title and thumbnail
I have created a custom post type which is 'post_type' => 'video' in my control displayed all the video categories now i want to display all selected that posts Title and Thumbnail in Elementor content_template() and render() function..
<?php
$options = array();
$args = array(
'hide_empty' => false,
'post_type' => 'video',
'taxonomy' => 'video_categories',
);
$categories = get_categories($args);
foreach ( $categories as $key => $category ) {
$options[$category->term_id] = $category->name;
}
$this->add_control(
'video_categories',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => $options,
]
);
}
// Want to display selected posts title and thumbnale in loop
protected function render() {
$settings = $this->get_settings_for_display();
foreach ( $settings['show_elements'] as $element ) {
echo '<div>' . $element . '</div>';
}
}
protected function _content_template() {
?>
<# _.each( settings.show_elements, function( element ) { #>
<div>{{{ element }}}</div>
<# } ) #>
<?php
}
}
?> ```
First of all, you have to change
$settings['show_elements']
to
$settings['video_categories']
$element showes only the IDs of the selected categories.
With this WP-functions you get i.e. the Name of the Category by ID in the render output:
get_cat_name( int $cat_id )
For getting a post loop of the selected categories, you have to look further.
Maybe somebody can improve it or check it.
As I did you can create two functions, one you will pass to the select2 control type
as the 'options' parameter which will populate only the 'value' and the 'text' within
the options tags. The code will look something like this:
//The add_control method that populates de select2 field
$this->add_control(
'filter_cloud_tags',
[
'label' => esc_html__( 'Select Tags', 'tw360-elementor-add-on'),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'default' => 'all',
'options' => $this->getControlTags(),
]
);
getControlTags() return an array with the options for the select2 field
like this value => name, the choosen values will be returned to pass them later into the render() method.
html returned by elementor in my case 'slug}">{tag->name}
/*The two functions -> */
protected function getControlTags()
{
$args = array(
'taxonomy' => 'post_tag',
'orderby' => 'count',
'order' => 'DESC',
'hide_empty' => true,
);
foreach (get_tags($args) as $tag):
$tags[$tag->slug] = $tag->name . "($tag->count)";
endforeach;
return $tags;
}
protected function getTags($limit = 10, $include = [])
{
$args = array(
'taxonomy' => 'post_tag',
'orderby' => 'count',
'order' => 'DESC',
'hide_empty' => true,
'include' => $include,
'number' => $limit,
);
$tags = get_tags($args);
return $tags;
}
And then in the render method you could pass some parameters to the second function which is
getTags($limit = 10, $include = []) limit = maximum number of tags to return and include = the specific tags to return
which in my case is by slug which I passed to the value attributes of the select2 options in the first add_control method
In my render function I check that if specicif tags are choosen then the 'limit' parameter will be the number of choosen tags.
If you do not set it to the number of choosen tags when specific tags are choosen then you may not see all the choosen tags when render.
//This is my render function
protected function render() {
$settings = $this->get_settings_for_display();
if(is_array($settings['filter_cloud_tags']) && count($settings['filter_cloud_tags']) > 0)
$limit = count($settings['filter_cloud_tags']);
else
$limit = $settings['tag_limit'];
$this->add_inline_editing_attributes('cloud_title','basic');
$this->add_render_attribute(
'wrapper', [
'class' => ['elementor-widget-wp-widget-tag_cloud'],
]
);
$this->add_render_attribute(
'cloud_title', [
'class' => ['tw360_sidewiget_title'],
]
);
?>
<div <?php echo $this->get_render_attribute_string('wrapper') ?>>
<?php if($settings['cloud_title']){ ?>
<h5 <?php echo $this->get_render_attribute_string('cloud_title') ?>><?php echo $settings['cloud_title'] ?></h5>
<?php } ?>
<div class="tagcloud">
<?php
foreach($this->getTags($limit, $settings['filter_cloud_tags']) as $tag){
if($settings['show_tag_count'] == 1){
?>
<?php echo $tag->name." (".$tag->count.")"; ?>
<?php }else{ ?>
<?php echo $tag->name; ?>
<?php } ?>
<?php } ?>
</div>
</div>
<?php
}
At last I leave you the add_control method that shows you the text field to set the limit and
a switcher in case you also want to show the number of posts for that tag.
$this->add_control(
'tag_limit',
[
'label' => esc_html__('Limit', 'tw360-elementor-add-on'),
'type' => \Elementor\Controls_Manager::TEXT,
'default' => 10
]
);
$this->add_control(
'show_tag_count',
[
'label' => esc_html__('Show Count', 'tw360-elementor-add-on'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'return_value' => '1',
'default' => '0'
]
);
Hope this is helpfull for someone.
If you modify it a little with the right wp methods it will work for categories, posts, custom posts and custom taxonomies...
Sorry this is not the right exact function you wanted but I needed an explatation like this long time ago and I think
you (the reeder) could make it even more usefull for you.
i am developing a multivendor shop and i want the vendors to post products from a Template page. i am using Advance custom fields to achieve this functionality.
What i did by far is to create the page template and managed to display the form but i have some issue to validate the fields with the Product. i might need to create some functionalities ?
<?php
/**
* The template for displaying all pages.
* Template Name: Add Product Vendor
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* #package Bober
*/
?>
<?php
acf_form_head();
get_header();
?>
<div id="content">
<?php
global $woocommerce;
acf_form(array(
$post_title = 'field_5a60022b402e2',
$post_categ = 'field_5a60028e402e3',
$post_descrip = 'field_5a600384402e4',
$post_img = 'field_5a6005e1402e7',
$post_price = 'field_5a61207ce5226',
/*'post_title' =>true,
'post_content' => true,*/
'uploader' => 'basic',
'post_id' => 'new_post',
'fields' => array($post_title, $post_descrip, $post_categ,$post_price, $post_img),
'new_post' => array(
'post_status' => 'draft',
'post_type' =>'product',
),
/*'fields' => array('field_5a60022b402e2', 'field_5a60028e402e3', 'field_5a600384402e4', 'field_5a6005e1402e7'),*/
'submit_value' => 'Add product'
));
?>
</div>
<?php get_footer(); ?>
At this point i was able to asign the title of the product and the content of by using the code below based on ACF form documentation.
'post_title' =>true,
'post_content' => true,
How do i assign the Title value, the description value, price value and image to the product post ? I don't necesary look for someone to provide me a code to replace, i want to know how to do it, give me some ideas, or where to read on how to do it.
Thank you in advance!
First, do an in-depth read of how acf_form() works.
The acf_form() code for adding a product in woocommerce could look like this:
$options = array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'new_post' => array(
'post_type' => 'product',
'post_status' => 'draft'
),
'field_groups' => [$field_group_id],
'form' => true,
'return' => '%post_url%', // so they could be redirected to the product page in a draft state - i imagine?!
'html_submit_button' => '<input type="submit" value="%s" />',
'updated_message' => 'Product Created',
'submit_value' => 'Submit Product'
);
acf_form($options);
Regarding the featured image of the product, create an image field and use the code below to sync the field with the featured image. You can read more about this by checking the ACF API reference for acf/update_value
add_filter('acf/update_value/name=featured_image', function ($value, $post_id, $field) {
if ($value != '') {
update_post_meta($post_id, '_thumbnail_id', $value);
} else {
delete_post_thumbnail($post_id);
}
return $value;
}, 10, 3);
Having a bit of bother with the Wordpress Meta Box plugin, specifically with retrieving an image url from an image added to a custom post type.
I'm creating meta boxes in a custom plugin, like so:
add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );
function xxx_meta_register_meta_boxes( $meta_boxes )
{
$prefix = 'xxx_meta_';
$meta_boxes[] = array(
'title' => esc_html__( 'Retailer Information', '' ),
'id' => 'advanced',
'post_types' => array( 'xxx_retailers' ),
'autosave' => true,
'fields' => array(
// PLUPLOAD IMAGE UPLOAD (WP 3.3+)
array(
'name' => esc_html__( 'Retailer Logo', '' ),
'id' => "{$prefix}plupload",
'type' => 'plupload_image',
'max_file_uploads' => 1,
),
// URL
array(
'name' => esc_html__( 'Link', '' ),
'id' => "{$prefix}url",
'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
'type' => 'url',
'std' => 'xxx',
),
)
);
return $meta_boxes;
}
So far so good, these boxes are relevant to a custom post type 'xxx_retailers'.
The problem comes with retrieving this data. I want to display my retailers in a widget. I've chopped and changed another piece of code I've used previously, but it's not returning the image URL, just the ID. Unfortunately I don't know enough php to figure out why.
// Create Retailers Widget
// Create the widget
class Retailers_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'retailers_widget',
// name of the widget
__('XXX Retailers List', '' ),
// widget options
array (
'description' => __( 'Shows a list of retailer logos', '' )
)
);
}
function widget( $args, $instance ) {
// kick things off
extract( $args );
echo $before_widget;
echo $before_title . 'Retailers' . $after_title;
// Pull through Retailers
$xxxretailers = get_posts(array(
'post_type' => 'xxx_retailers',
'orderby' => 'title',
'order' => 'asc',
));
// Display for each Retailer
foreach ($xxxretailers as $xxxretailer) {
$custom = get_post_custom($xxxretailer->ID);
$meta_ret_img = $custom["xxx_meta_plupload"][0];
$meta_ret_url = $custom["xxx_meta_url"][0];
// Display Retailers
echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
}
}
};
// Register widget
function register_retailers_widget() {
register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );
The URLs are coming through correctly, so I know this is a problem with the line
$meta_ret_img = $custom["xxx_meta_plupload"][0];
But I can't figure out how to get the image URL from the data I presume is stored as an array. Any ideas?
Edit:
I should have mentioned, in a single post I can get a single image with:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
But I want to show images from all my retailers post types, to create a list of logos.
Replace this statement:
$meta_ret_img = $custom["xxx_meta_plupload"][0];
with this:
$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);
$meta_ret_img = $meta_ret_img_array[0];
also please remove all these curly braces from src and href attributes from your code.
if you are interested in any particular size of the image, then see the official document of wp_get_attachment_image_src() function here.
For e.g. for medium size image you can write it as:
wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');
I'm using Visual Composer in WordPress and I want to make a custom Post Grid. But the default elements that the post grid supplies are not enough. I want to show the author of the post, the number of comments it has, the category it has and the Tags it has as well. I'm not really familiar with Visual Composer, but I need a point in the right direction for me to get this data? What can I do? I've search their documents but with no luck. If I need to move around the php code I would like to know what I'm moving around is the right thing. Any ideas? If you need any more information please do ask :D
Thanks in advance for the help.
If anybody is still looking to find out how to to get the id in a post grid or create a specific widget for the grid you can use the following snippet I creatd for adding an icon before the post title. You will see inside the first function you can call $post-ID for use on any query.
//** Case Study Title Block Shortcodes ***********
//********************************
add_filter( 'vc_gitem_template_attribute_case_study_title','vc_gitem_template_attribute_case_study_title', 10, 2 );
function vc_gitem_template_attribute_case_study_title( $value, $data ) {
extract( array_merge( array(
'post' => null,
'data' => '',
), $data ) );
$atts_extended = array();
parse_str( $data, $atts_extended );
$atts = $atts_extended['atts'];
// write all your widget code in here using queries etc
$title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$terms = get_the_terms($post->ID, 'case_categories');
$output = "<h4 class=\"case-title\">". get_the_icon($terms[0]->term_id) . $title ."</h4>";
return $output;
}
add_filter( 'vc_grid_item_shortcodes', 'case_study_title_shortcodes' );
function case_study_title_shortcodes( $shortcodes ) {
$shortcodes['vc_case_study_title'] = array(
'name' => __( 'Case Study Title', 'sage' ),
'base' => 'vc_case_study_title',
'icon' => get_template_directory_uri() . '/assets/images/icon.svg',
'category' => __( 'Content', 'sage' ),
'description' => __( 'Displays the case study title with correct icon', 'sage' ),
'post_type' => Vc_Grid_Item_Editor::postType()
);
return $shortcodes;
}
add_shortcode( 'vc_case_study_title', 'vc_case_study_title_render' );
function vc_case_study_title_render($atts){
$atts = vc_map_get_attributes( 'vc_case_study_title', $atts );
return '{{ case_study_title }}';
}
I got the same issue; here is how I solve it:
According to Visual Composer documentation: https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
When you add the code below to functions.php, a new component will be added in your custom grid builder (in my example the name will be "Author"). There are a number of values you can get by post data template variable function and one of it not the name of the author but their ID (that's sad but at least you can use this value to get the author name).
The value is 'post_author' => ID of the author (for example '1')
Here is the function where I get the post author and display it (if author component was added to your custom grid in "custom grid builder"). Put it in functions.php of your child theme:
add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_post_id'] = array(
'name' => __( 'Author', 'my-text-domain' ),
'base' => 'vc_post_id',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Show current post author', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
// output function
add_shortcode( 'vc_post_id', 'vc_post_id_render' );
function vc_post_id_render() {
$nn = '{{ post_data:post_author }}'; // usage of template variable post_data with argument "post_author"
return get_the_author($nn);
}
There is a little problem. It works but get_the_author is a deprecated function in WordPress. I'd appreciate any suggestions to make it more modern or if you name other alternatives please suggest.
Also, here is the list of available variables of vc_post_id_render from docs. Here they are:
WP_Post::__set_state(array(
'ID' => 69,
'post_author' => '1',
'post_date' => '2015-04-29 14:15:56',
'post_date_gmt' => '2015-04-29 14:15:56',
'post_content' => 'Your post content',
'post_title' => 'Your post title',
'post_excerpt' => '',
'post_status' => 'publish',
'comment_status' => 'open',
'ping_status' => 'open',
'post_password' => '',
'post_name' => 'post name',
'to_ping' => '',
'pinged' => '',
'post_modified' => '2015-06-17 11:18:41',
'post_modified_gmt' => '2015-06-17 11:18:41',
'post_content_filtered' => '',
'post_parent' => 0,
'guid' => 'http://wp.master/?p=69',
'menu_order' => 0,
'post_type' => 'post',
'post_mime_type' => '',
'comment_count' => '0',
'filter' => 'raw',
'filter_terms' =>
array (
),
))
this is your response
https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/
at Template variables usage
Exemple, in visual composer template you can use {{ post_date:ID }} to show post ID. I don't know how show tag.
I'm having trouble displaying two custom drop-down menus in the WP admin bar. What I want is the first drop-down to display the back-end (editing part) links of every page and the other to display the front-end links of every page. Right now, only the last foreach statement being called displays. Any suggestions are much appreciated.
function admin_bar_link($admin_bar) {
if ( !is_super_admin() || !is_admin_bar_showing() )
return;
$pages = get_pages();
$nurs = get_site_url();
$admin_bar->add_menu(array(
'id' => 'this',
'title' => __($nurs),
'href' => $nurs
));
$admin_bar->add_menu(array(
'id' => 'edit_pages_links',
'title' => __('Edit Pages'),
'href' => false
));
$admin_bar->add_menu(array(
'id' => 'view_pages_links',
'title' => __('View Pages'),
'href' => false
));
foreach ( $pages as $page ) {
$title = $page->post_title;
$url = get_permalink ( $page->ID ) . 'wp-admin/post.php?post=' . $page->ID . '&action=edit'; //edit post url
$admin_bar->add_menu (array(
'title' => $title,
'href' => $url,
'parent' => 'edit_pages_links'
)
);
}//end foreach
foreach ( $pages as $page ) {
$title = $page->post_title;
$url = get_permalink ( $page->ID ) . '?p='. $page->ID;
$admin_bar->add_menu ( array (
'title' => $title,
'href' => $url,
'parent' => 'view_pages_links'
)
);
}//end foreach
}
add_action('admin_bar_menu', 'admin_bar_link');
Firstly your function should accept the $admin_bar variable like this:
function admin_bar_link($admin_bar)
and you don't need global $wp_admin_bar anymore. Then replace these:
$wp_admin_bar->add_menu
with this
$admin_bar->add_menu
Then the menus should appear on the admin bar.