1

I have a custom post type called 'subjects' I want to wrap all the content inside it in the follwoing div

`<div id="link_count">`

content 

`</div>`

so that any post in 'subjects' would be automatically wrapped in this div as I use it for tracking purposes and I have 500 posts and I don't want to it manually

how can I do that? Thanks in advance

2
  • more clarification needed to answer?
    – Anwer AR
    Commented Mar 5, 2017 at 20:06
  • sorry I edited the code, can you check it now? Commented Mar 5, 2017 at 20:13

1 Answer 1

0
if (is_singular('subjects')) {

    echo '<div id="link_count">';

    the_content(); 

    echo '</div>';  //end of link_count div

}else{

    the_content();

}

If this is just simplified code for the question, but you're going to have more items in and around the_content, you can just wrap 2 of the if statements around the call for the div and end of the div...

if (is_singular('subjects'))

    echo '<div id="link_count">';

the_content(); //and whatever else goes here.

if (is_singular('subjects'))

    echo '</div>';  //end of link_count div.

In either case you'll want to make sure that the CPT is actually "subjects" and not namespaced when it was created or this won't work. ie you'll need the right name of the cpt as registered in the if statement.

if you want to add to a functions.php, try this. It's not tested but should work:

function add_content_link_count( $content ) {

if (is_singular('subjects')) { 
     $custom_content = '<div id="link_count">'. $content . '</div>';
    } else {
        $custom_content = $content;
    }
return $custom_content;
}
add_filter( 'the_content', 'add_content_link_count' );
4
  • I see, great answer ^^, where should I put it? in the theme's function.php? Commented Mar 5, 2017 at 21:06
  • This would go in your index.php or your single.php. are you looking to add to your functions.php instead?
    – rudtek
    Commented Mar 5, 2017 at 21:15
  • yeah as I don't want to touch wordpress core files because automated updates are enabled so it will be reverted to the original. plus maintaining the code in a child's theme function.php is a bit easier Commented Mar 5, 2017 at 21:17
  • your index and single php files should be in your child theme as copied from your regular theme to avoid just this... However I put a version in my answer that will work for the functions.php as well. Let me know.
    – rudtek
    Commented Mar 5, 2017 at 21:28

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.