The best way to achieve this would be using a shortcode or a filter.
I tend to use the Shortcode in this scenario, because you are flexible to add another paragraph behind the autoatically generated content, this is more complicated as a filter. Also, I do not know if your Cities are Custom Post Types, or if there is another way WordPress could know that this is a City and not some other content (using a meta_value
, page_template
, …).
The function to create the content
However, the function to generate the content is fairly simple. I added __()
to make the content translateable, as you should do with every hardcoded string.
function f711_generate_city_texts( $cityname ) { // function receives cityname as variable
return $cityname . __( 'is a beautiful city', 'f711_wpse' ); //returns the custom content with the cityname built in.
}
Now, we need to pass add this function to the content.
Adding the shortcode
I create a Shortcode function, and add the shortcode:
add_shortcode( 'f711_citytext', 'f711_citytext_shortcode' );
function f711_citytext_shortcode( $atts ) {
extract( shortcode_atts( array(
'city' => get_the_title(), // define city as a attribute for the shortcode, with the title as standard value
), $atts ) );
return f711_generate_city_texts( $city );
}
In your content, you can now use
[f711_citytext city="yourcityname"]
or you could leave the city
-attribute, if you are within the loop, as get_the_title()
is used as the standard value in the function.
Adding a filter
Of course, you could also add a filter to add the text automatically, as I mentioned you need a way of knowing that this content belongs to a city in this case.
I will use a meta_value
containing a bool
with f711_iscity
set to true
.
add_filter( 'the_content', 'f711_citytext_filter', 10, 1 );
function f711_citytext_filter ( $content ) {
if ( get_post_meta( get_the_ID(), 'f711_iscity', true ) === true )
return $content . f711_generate_city_texts( get_the_title() );
return $content;
}
This function checks if you set f711_iscity
to true
, and returns the content with the appended generated text. Otherwise it leaves the content untouched.
Conclusion
For flexibility reasons, I would prefer the shortcode soluiton in this case.