I register a shortcode on my functions.php
function offer_intro_shortcode ($atts, $content=null) {
$offer = shortcode_atts( array (
'title' => '',
'text' => '',
), $atts );
extract ($offer);
return '<div class="center gap">
<h3>'.$title.'</h3>
<p class="lead">'.$text.'</p>
</div>';
}
add_shortcode ('offer', 'offer_intro_shortcode');
Then i write on wordpress post:
[offer title='What We Offer' text='Look at some of the recent projects we have completed for our valuble clients']
Then i query for this shortcode on my index.php like do_shortcode('[offer]')
But it is not working
try this.
echo do_shortcode("[offer title='What We Offer' text='Look at some of the recent projects we have completed for our valuble clients']");
check this is working or not.
After $atts add another parameter i.e shortcode so it would become.
$offer = shortcode_atts( array (
'title' => '',
'text' => '',
), $atts,'offer' );
Related
I am using Wordpress and PHP. I declared a custom sidebar using this code :
function customtheme_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'My custom sidebar', 'customtheme' ),
'id' => 'my-custom-sidebar',
'description' => esc_html__( '.', 'customtheme' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'customtheme_widgets_init' );
Ok so In my code I want to get my sidebar and store it in a PHP variable $the_widget. Using this code :
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
$the_widget = dynamic_sidebar('my-custom-sidebar');
endif;
when i do that it automatically echoes the widget(sidebar) when i call dynamic_sidebar() can i do that without the echo?? is there another function in the wordpress codex that can do the same?
No unfortunately there isn't a WordPress function such as get_dynamic_sidebar() however a common method for getting the sidebar into a variable as a string is this:
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
ob_start();
dynamic_sidebar('my-custom-sidebar');
$the_widget = ob_get_contents(); //or ob_get_clean();
ob_end_clean();
endif;
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 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>";
}
I'm using the wp_get_archives function in Wordpress.
<?php $args = array(
'type' => 'monthly',
'limit' => '',
'format' => 'html',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC'
); ?>
<?php wp_get_archives( $args ); ?>
But I want to be able to change the URL - rather than it going to my Wordpress installation directory, how can I change it to my own custom URL?
PS: I found on the general-template.php file starting at line 937 is where this function starts.
I saw that, inside the function wp_get_archives, the links are built with the function get_archives_link(). There, we find the filter hook get_archives_link that returns each HTML link for the archives.
So, we can manipulate each of this strings and replace stuff:
add_filter( 'get_archives_link', function( $html )
{
if( is_admin() ) // Just in case, don't run on admin side
return $html;
// $html is '<li><a href='http://example.com/hello-world'>Hello world!</a></li>'
$html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );
return $html;
});
brasofilo's solution worked for me, with some minor adjustments.
before:
$html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );
after:
$html = str_replace( 'http://example.com', 'http://example.com/the_events', $html );
I am trying to create a 'Pie Chart' shortcode to use in WP. All works fine apart from the percentage number. If it is entered into the array it works fine, but if I remove that number (ie; 100 - as seen in the code below), any number that is entered on the front-end by the user returns empty?? Quite puzzling?
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data_percentage' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data_percentage .'"><span>'.$data_percentage.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );
And this is the shortcode that needs to be entered on the front-end -
[piechart_inner data-percent="45" title="WordPress"][/piechart_inner]
Which outputs nothing for the data-percent, whatever value is entered?
Many thanks
You are using the wrong variable. You are giving data-percent when you have variable data_percentage
Your shortcode should look like this:
[piechart_inner data_percentage="45" title="WordPress"][/piechart_inner]
Or change the function to following:
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data-percent' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data-percent .'"><span>'.$data-percent.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );