I'm trying to learn how to use the Custom Field function. I have a series of posts with titles like "Adams County," "Lane County," etc. All are in the category "Washington" and are tagged with "county."

I want to display the following on each page:

"If you live in $MyCounty, tell us what you think."

$MyCounty would echo the page title (e.g. Adams County). I can easily do this with a PHP include on my regular sites. However, I've been advised that I should use Custom Fields and Shortcodes with WordPress.

I created a Custom Field titled "County Intro." I've been advised to use the following code:

function show_county() {
  global $post;
  // assumes the string is in the custom field 'county'
  $county = get_post_meta( $post->ID, 'county');
  if ( $county ) {
    return $county;
add_shortcode( 'county', 'show_county');

But I don't even know where to insert the code, other than "the loop." Can someone tell me the name of the file I need to open? I'm using a child theme; do I need to copy this file from the theme folder into the child theme folder?

  • if you want the text to reflect the text title, then just use it. There is no need to add the mental overhead of using custoom fields if the information is already there at some other part of the post Commented Jun 9, 2016 at 4:51

2 Answers 2


I would not use custom fields or shortcodes for this functionality, I would look at filters. the_content filter and the loop_end action (just make sure that your theme uses while ( have_posts() ) in single post pages) comes to immediate mind here. These are two options which requires no modification of any of your files in your theme. This way you can add extra content to your single post pages (which I assume is what you need) without having to create a child theme.

Approaching your problem this way, you also do not need to remember to use the shortcode or set the custoim field for a particular post, so you have a more dynamic way which you set once and forget about it

For conveniency, and also the recommended way, is to create your own custom plugin in which you can dump the code. This way, even if you switch themes, your custom text will still show without having to modify the new theme or create yet another child theme.

Lets look at the two methods to add your custom text line: (NOTE: All code is untested and should go into a custom plugin)

the_content FILTER

This will add your custom text after the content on your single post page

add_filter( 'the_content', function ( $content )
     * Only filter the content on the main query, and when the post belongs
     * to the washington category and are tagged county. Adjust as needed
    if ( !is_single() )
        return $content;

    if ( !in_the_loop() )
        return $content;

    $post = get_queried_object();
    if (    !in_category( 'washington', $post ) 
         && !has_tag( 'county', $post )
        return $content;

    // We are inside the main loop, and the post have our category and tag
    // Get the post title
    $MyCounty = apply_filters( 'the_title', $post->post_title );
    // Set our custom text
    $text = "If you live in $MyCounty, tell us what you think.";

    return $content . $text;

loop_end ACTION

This will add your text after the post, you can also use loop_start to add the text before the loop, or even the_post which will also add the text before the post. Just note, all actions needs to echo their output

add_action( 'loop_end', function ( \WP_Query $q )
    // Only target the main query on single pages
    if (    $q->is_main_query()
         && $q->is_single()
    ) {
        $post = get_queried_object();
        if (    in_category( 'washington', $post )
             && has_tag( 'county', $post )
        ) {
            $MyCounty = apply_filters( 'the_title', $post->post_title );
            $text     = "If you live in $MyCounty, tell us what you think.";

            // Echo our custom text
            echo $text;

Quickest way is actually to make a standalone php file (wrap the code in <?php CODE ?> and save it in your /wp-content/mu-plugins/ directory, eg, I often use a site-shortcodes.php...

WordPress will auto-load the file, and your functionality will be independent of the theme you use. otherwise if the functionality is tied in to your theme specifically somehow you could use your child theme's functions.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.