$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Show own shortcode data on each page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Show own shortcode data on each page

matteradmin9PV0评论

I have shortcode [cities] and i need that when i import my pages to wordpress with text with this shortcode, there was own city by page id. Is it possible to do?

I created a simple shortcode:

add_shortcode ('cities', 'show_cities');
function show_cities(){
    return "New York";
}

Think that i need some array with cities and page id's but i don't understand how to do this. Can you give me a hint?

I have shortcode [cities] and i need that when i import my pages to wordpress with text with this shortcode, there was own city by page id. Is it possible to do?

I created a simple shortcode:

add_shortcode ('cities', 'show_cities');
function show_cities(){
    return "New York";
}

Think that i need some array with cities and page id's but i don't understand how to do this. Can you give me a hint?

Share Improve this question asked Mar 14, 2019 at 8:39 WopWop 132 bronze badges 2
  • Hi, could you explain a little bit more what exactly do you want to achieve? How should that shortcode work? Some examples would be great too, I guess... – Krzysiek Dróżdż Commented Mar 14, 2019 at 8:41
  • For example: i have 100 pages with special offers for cities. On each page unique city and unique offer. I need to put on each page shortcode [cities] for show right city for this page by page id. If page id 7 then show New York, if page id 10 > show Alabama – Wop Commented Mar 14, 2019 at 8:46
Add a comment  | 

1 Answer 1

Reset to default 0

Best way to achieve the desired functionality is to use post_meta / custom field.

add_shortcode ('cities', 'show_cities');
function show_cities(){
   /* 
      Create a custom field 'city' to save city name in page editor
   */

   $city = get_post_meta( get_the_id(), 'city', true );

   return $city;
}

Using an array can also do the work as under:

add_shortcode ('cities', 'show_cities');
function show_cities(){
   /* 
      Create an array using Page_id as index, e.g.
      $cities [ 'page_id' ] = "City Name";
   */

   $cities [ 7 ] = "New York";
   $cities [ 10 ] = "Alabama";

   return $cities [ get_the_id() ];
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far