I'm trying to use the Advanced Custom Fields plugin shortcode as a attribute in another shortcode.
The company my theme was made by is not offering custom support at this time.
The original shortcode is made list this
public function plan( $atts, $content )
{
$html = array();
extract( shortcode_atts( array(
'price' => '',
'price_info' => '',
'type' => '',
'delay' => '',
'class' => '',
), $atts ) );
$html[] = '<div class="' . ( $class ) . '">';
$html[] = '<div class="plan has-animation" data-delay="' . ( (int)$delay ) . '">';
$html[] = '<div class="plan-container">';
$html[] = '<ins class="plan-price">' .( $price ) . '</ins>';
$html[] = '<span class="price-info">' . ( $price_info ) . '</span>';
$html[] = '<h2 class="second_color">' . ( $type ) . '</h2>';
$html[] = do_shortcode( $content );
$html[] = '</div>';
$html[] = '</div>';
$html[] = '</div>';
return implode("\n", $html);
}
And when i call the shortcode [pt_plan price="$12.99" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"] I want to make the price attribute allow a shortcode as a value to take advantage of Advanced Custom Fields.
However, if I place [acf field='session_price_1'] as the attribute value, like this, [pt_plan price="[acf field='session_price_1']" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"] it comes out as if that was a string and breaks the original pt_plan shortcode.
Could someone point me in the right direction? I thought it would be to add do_shortcode() to the $price variable in the plan shortcode, but it did not work.
Hope all this makes sense and thanks in advance!!
I actually got it to work.
Because the way shortcodes work in WP, the ending ] of the nested shortcode was closing off the original shortcode, screwing everything up.
To get around this, I did the following:
$html[] = '<div class="plan-price">'.do_shortcode('['.$price.']').'</div>';
and
[pt_plan price="acf field='session_price_1'" price_info="per month" type="Standard" delay="400" class="col-lg-4 col-md-4 col-sm-4"]
and that worked great!
Basically I just typed the text of the shortcode and ' instead of " into the attribute, concatenated the [ and ] in when it prints the attribute value into the , and used do_shortcode() to run the shortcode.
Related
Is it possible to manipulate the output of the image captions within the_content in such a way that not only the caption, but also the image title and/or description (defined in the media library) are output under each image?
Possibly via the functions.php?
Yes it is possible to manipulate the on the_content.
The easiest way which it looks like you are wanting to do is add a filter in your functions.php file.
I just pulled this from the Codex and modified a bit so you can get the gist of what I'm talking about.
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $output, $attr, $content ) {
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
. '</div>';
}
What you can then do is if you see the $attr['caption'], you can modify how you would like it to be. So you can do: get_the_title($attr['id']) to add the title or get_post_meta($attr['id'], '_wp_attachment_image_alt', TRUE); to get the image alt.
To make things easier if you want to grab all the details on a specific attachment you can use a function like this and then get whatever you want.
Here's an example of adding the title to your caption. The format is "Title - Caption"
. '<p class="wp-caption-text">' . get_the_title($attr['id']) . " - " . $attr['caption'] . '</p>'
Link to Code reference: https://developer.wordpress.org/reference/hooks/img_caption_shortcode/
Maybe i'm not understanding your question but it seems to me that you're using the Gutenberg editor?
... All the features you're looking for are already built-in.
If you want to set the alt="" or title="" attributes you can do it on the right side action column, by clicking on the image itself, while in the Gutenberg editor.
For the figcaption, you can set it right under the image, by clicking on the image itself, while in the Gutenberg editor.
This below code is working for post and not for category:
function featuredtoRSS($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'medium', array(
'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'featuredtoRSS');
add_filter('the_content_feed', 'featuredtoRSS');
How to modify it?
assume that all your posts are come from single.php. WordPress provides has_category() function to check if post has specific category.
So your code on single.php will need to be like something in given below
if( has_category('video') ){
the_post_thumbnail();
}
where it checks if post has 'video' category. And if it is true then the feature image will be displayed. otherwise, it can't. Hope it will be help you.
If you want to use a plugin you can use a plugin. If the plugin does not support displaying images in the rss feed you can add this in the function.php file. Something like:
function cattitlerss($content) {
$postcat = "";
foreach((get_the_category()) as $cat) {
$postcat .= ' ('.$cat->cat_name . ')';
}
$content = do_shortcode('[wp_custom_image_category onlysrc="false" size="full" term_id="123" alt="alt :)"]');
return $content;
}
add_filter('the_title_rss', 'cattitlerss');
How can I create a code which would search by slug(category name) but not by ID. How can I create a customized solution for my client for search within a specific category (without any plugins)?
Explain: I want the code of search to be customized in such a way that the client could search within a specific category without editing any code. Thanks!
is it's still actually, you should to check this tutorial where an author gives a very detailed explanation about the Search function customization.
One of the most interesting parts for a necessary your case:
// meta_query expects nested arrays even if you only have one query
$sm_query = new WP_Query( array( 'post_type' => 'accommodation', 'posts_per_page' => '-1', 'meta_query' => array( array( 'key' => '_sm_accommodation_city' ) ) ) );
// The Loop
if ( $sm_query->have_posts() ) {
$cities = array();
while ( $sm_query->have_posts() ) {
$sm_query->the_post();
$city = get_post_meta( get_the_ID(), '_sm_accommodation_city', true );
// populate an array of all occurrences (non duplicated)
if( !in_array( $city, $cities ) ){
$cities[] = $city;
}
}
}
} else{
echo 'No accommodations yet!';
return;
}
/* Restore original Post Data */
wp_reset_postdata();
if( count($cities) == 0){
return;
}
asort($cities);
$select_city = '<select name="city" style="width: 100%">';
$select_city .= '<option value="" selected="selected">' . __( 'Select city', 'smashing_plugin' ) . '</option>';
foreach ($cities as $city ) {
$select_city .= '<option value="' . $city . '">' . $city . '</option>';
}
$select_city .= '</select>' . "\n";
reset($cities);
I have been searching for weeks and I still haven't found a proper solution to this problem.
I am writing a Wordpress Theme. I have a custom post type called Works. I would like to add my Works archive to my menu and have it as well as it's posts highlighted when I access them.
I can access my archive and posts on the following links
Works archive: /works/
Works single post: /works/postname/
My solution so fare have been to name my archive-works.php template file with a template name (Work archive). Then create an empty page using that template and adding the page to the menu. This highlights the archive in the menu but not the single posts.
I can easily solve this with a custom link and some javascript but there must be a better and cleaner way.
You can do a simple trick in your functions.php:
add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($classes, $item) {
// Get post_type for this post
$post_type = get_query_var('post_type');
// Go to Menus and add a menu class named: {custom-post-type}-menu-item
// This adds a 'current_page_parent' class to the parent menu item
if( in_array( $post_type.'-menu-item', $classes ) )
array_push($classes, 'current_page_parent');
return $classes;
}
In your case, you just have to add a class 'works-menu-item' with that archive menu item by the admin panel;
To add "custom posttype archive link" to menu, please look at the following guide
Open file functions.php , and enter code below
add_action('admin_head-nav-menus.php', 'wpclean_add_metabox_menu_posttype_archive');
function wpclean_add_metabox_menu_posttype_archive() {
add_meta_box('wpclean-metabox-nav-menu-posttype', __('Custom Post Type Archives'), 'wpclean_metabox_menu_posttype_archive', 'nav-menus', 'side', 'default');
}
function wpclean_metabox_menu_posttype_archive() {
$post_types = get_post_types(array('show_in_nav_menus' => true, 'has_archive' => true), 'object');
if ($post_types) :
$items = array();
$loop_index = 999999;
foreach ($post_types as $post_type) {
$item = new stdClass();
$loop_index++;
$item->object_id = $loop_index;
$item->db_id = 0;
$item->object = 'post_type_' . $post_type->query_var;
$item->menu_item_parent = 0;
$item->type = 'custom';
$item->title = $post_type->labels->name;
$item->url = get_post_type_archive_link($post_type->query_var);
$item->target = '';
$item->attr_title = '';
$item->classes = array();
$item->xfn = '';
$items[] = $item;
}
$walker = new Walker_Nav_Menu_Checklist(array());
echo '<div id="posttype-archive" class="posttypediv">';
echo '<div id="tabs-panel-posttype-archive" class="tabs-panel tabs-panel-active">';
echo '<ul id="posttype-archive-checklist" class="categorychecklist form-no-clear">';
echo walk_nav_menu_tree(array_map('wp_setup_nav_menu_item', $items), 0, (object) array('walker' => $walker));
echo '</ul>';
echo '</div>';
echo '</div>';
echo '<p class="button-controls">';
echo '<span class="add-to-menu">';
echo '<input type="submit"' . disabled(1, 0) . ' class="button-secondary submit-add-to-menu right" value="' . __('Add to Menu') . '" name="add-posttype-archive-menu-item" id="submit-posttype-archive" />';
echo '<span class="spinner"></span>';
echo '</span>';
echo '</p>';
endif;
}
Thanks
Thanks to rasmussvanejensen for her/his nice question and thethangtran for the answer, I am still confused why Wordpress has not yet added such a good feature to its code base by default.
By the way I think there is even a better solution than the one provided by thethangtran, as it may break on some situations.
Note 1
According to the Codex, using register_post_type, one can add extra post_types to the installation. There is chance, some one need to change the 'query_var' and thus the provided code will break.
Note 2
In addition, it may not handle the current-menu-item class, which will be used for css customization to show the menu item as active.
Note 3
As another note on the code, there is no need to define the loop_index, item and items variables. they are absolutely useless.
A better solution
So I suggest using this alternative, for those who want a more robust solution to on this:
function prefix_add_metabox_menu_posttype_archive(){
add_meta_box( 'prefix_metabox_menu_posttype_archive', __( 'Archives' ), 'prefix_metabox_menu_posttype_archive', 'nav-menus', 'side', 'default' );
}
add_action( 'admin_head-nav-menus.php', 'prefix_add_metabox_menu_posttype_archive' );
function prefix_metabox_menu_posttype_archive(){
$post_types = get_post_types( array( 'show_in_nav_menus' => true, 'has_archive' => true ), 'object' );
if( $post_types ){
foreach( $post_types as $post_type ){
$post_type->classes = array( $post_type->name );
$post_type->type = $post_type->name;
$post_type->object_id = $post_type->name;
$post_type->title = $post_type->labels->name;
$post_type->object = 'cpt_archive';
}
$walker = new Walker_Nav_Menu_Checklist( array() );?>
<div id="cpt-archive" class="posttypediv">
<div id="tabs-panel-cpt-archive" class="tabs-panel tabs-panel-active">
<ul id="ctp-archive-checklist" class="categorychecklist form-no-clear"><?php
echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $post_types ), 0, (object) array( 'walker' => $walker ) );?>
</ul>
</div>
</div>
<p class="button-controls">
<span class="add-to-menu">
<input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-ctp-archive-menu-item" id="submit-cpt-archive" />
</span>
</p><?php
}
}
function prefix_cpt_archive_menu_filter( $items, $menu, $args ){
foreach( $items as &$item ){
if( $item->object != 'cpt_archive' ) continue;
$item->url = get_post_type_archive_link( $item->type );
if( get_query_var( 'post_type' ) == $item->type ){
$item->classes []= 'current-menu-item';
$item->current = true;
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'prefix_cpt_archive_menu_filter', 10, 3 );
Navigate to Appearance > Menus;
Make sure you have the Works' custom post type selected at Screen Options;
Click on the name of your custom post type to expand it and then click on the ‘View all’ tab;
You will see an option for All Works. Check the box next to it and then click on the Add to Menu button;
Your custom post type archive will now appear as a menu item in the right column;
By default, the label will be "All Works". You can change this by writing something different at the Navigation Label;
Click on the Save Menu button to save your changes.
I found a solution on GitHub and it works out of the box by simply adding it to the functions.php without any walkers or extra classes.
This code solves it by comparing the slug of the current post type
with the navigation items, and adds a class accordingly.
Code on GitHub
I want to change the template of my Ubercart products. More in detail, I want to add the label "From..." in front of each price. But I cannot find the template to do it.
I'm also using theme developer module and this is what I get:
zen_uc_product_price <
phptemplate_uc_product_price <
theme_uc_product_price
But I cannot find such files.
thanks
You can also make a node-product.tpl.php file, if so this is the way to get prices:
<?php
$context = array(
'type' => 'product',
'revision' => 'altered', // using altered to get the bare price with no themeing
'field' => 'sell_price',
'subject' => array('node' => $node)
);
$dp = uc_price($node->sell_price, $context);
$context['field'] = 'list_price';
$lp = uc_price($node->list_price, $context);
?>
<div class="price clear-block <?php if($dp != $lp) print 'discounted'; ?>">
<?php print 'From: ' . $node->content['display_price']['#value']; ?>
<?php //print $node->content['list_price']['#value']; ?>
</div>
This became a bit more than it should be:
Use:
content['display_price']['#value']; ?>
Unless you want to theme discounted pricing :-)
Just copied out from one of my projects.
Last: you can probably use theme_uc_product_price:
you add a function in template.php (Pasting in default implementation from uc_product.module
function zen_uc_product_price($price, $context, $options = array()) {
$output = '<div class="product-info '. implode(' ', (array)$context['class']) .'">';
$output .= uc_price($price, $context, $options);
$output .= '</div>';
return $output;
}
Inspect the $context variable for when to add the "From" part.
To add a prefix or suffix to the price, you can go to
/admin/store/settings/store/edit/format