最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating custom shortcode

programmeradmin2浏览0评论

I'm trying to create some custom shortcode - I want to be able to have names and numbers of people I want to display on my website, but as a shortcode, so it doesn't have to be typed in again and again.

So for instance to have [joe] relate to 'Joe Bloggs - 01925 265646'

I'm currently using the Ultimate Tables plugin and would want to put the shortcode into this, and then for it to come up with the longer information.

I've been playing around for a few hours and still struggling - I've tried putting it directly into functions.php and have tried using the 'Shortcodes Pro' plugin but I can't figure out how to make it work (sorry, I'm not a massively advanced Wordpress user!).

This is the code I put into the functions.php section of the theme:

// Add Shortcode
function joe() {
}
add_shortcode( 'Joe Bloggs - 01925 265646', 'joe' );

Pretty much, my aim is to avoid having to manually enter phone numbers because on our old website this led to the phone numbers being entered incorrectly sometimes, as they are entered about dozens of times.

Thanks so much for your help in advance.

I'm trying to create some custom shortcode - I want to be able to have names and numbers of people I want to display on my website, but as a shortcode, so it doesn't have to be typed in again and again.

So for instance to have [joe] relate to 'Joe Bloggs - 01925 265646'

I'm currently using the Ultimate Tables plugin and would want to put the shortcode into this, and then for it to come up with the longer information.

I've been playing around for a few hours and still struggling - I've tried putting it directly into functions.php and have tried using the 'Shortcodes Pro' plugin but I can't figure out how to make it work (sorry, I'm not a massively advanced Wordpress user!).

This is the code I put into the functions.php section of the theme:

// Add Shortcode
function joe() {
}
add_shortcode( 'Joe Bloggs - 01925 265646', 'joe' );

Pretty much, my aim is to avoid having to manually enter phone numbers because on our old website this led to the phone numbers being entered incorrectly sometimes, as they are entered about dozens of times.

Thanks so much for your help in advance.

Share Improve this question edited Sep 13, 2014 at 17:12 cybmeta 20.6k5 gold badges47 silver badges57 bronze badges asked Sep 13, 2014 at 17:05 ConnorConnor 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 1

You are passing the parameters wrong, try with something like this:

function get_telephone($attrs) {
$attr = shortcode_atts( array(
      'name' => 'John Doe'
  ), $attrs );
return attr['name'];
}
add_shortcode( 'telephone', 'get_telephone' );

In the post you call the shortcode like this: [telephone name="Jose"]

Anyway, inside the get_telephone function you should add some logic to get the phone numbers from somewhere, it could be an array, or you could create a custom post type to store the numbers.

Connor, I had a similar problem with trying to display product prices as shortcodes. Here is what I did.
1. created a custom table in the wordpress database (called it wp-products)
2. this table had the following fields: id, make, model, price
3. Created a shortcode to retrieve the price. Shortcode looks like this [product_price id=2] where id=2 is the price value for item 2 in the table.
4. wrote the shortcode function for this and put it in functions.php in my themes directory.
5. added the shortcode into the page location where I wanted to display the code.

Here is the code, I hope it helps

function product_price_func( $atts ) {
    global $wpdb;

    $table_name = $wpdb->prefix . 'products';

    $atts = shortcode_atts(
    array(
      'id' => 'no id found',
    ), $atts, 'product_price' );

    $product_id = $atts['id'];

    $product_price = $wpdb->get_results("SELECT price FROM " . $table_name ." WHERE id={$product_id}", ARRAY_A);

    foreach ( $product_price as $the_price ) 
      { 
        $output = $the_price['price'];
      }

    return "Product Price: $" . $output;
}
add_shortcode( 'product_price', 'product_price_func' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论