In some cases, we need to add some code to posts and pages but it needs to be in specific authored posts or pages only.
If you are uncomfortable editing or customizing a plugin just for this, ACF or Advance Custom Fields is one of the friendliest ways to do it.
I know there can be a plugin that does this already. But having to install a plugin just to install a simple functionality can take a toll of unnecessary code to your theme. Better yet, make sure that you care about a separate child theme in doing customizations to your website. This way, when the theme auto-updates, it won’t erase any customization you’ve done in your theme codes.
Steps On How To Add Custom Codes to Author Posts or Pages
Install ACF or Advanced Custom Fields
Go to Advanced Custom Fields plugin website. Download and install it to your WordPress website. Go to Plugins >> Add New >> Upload Plugin. After uploading, activate it.
Create the Custom Field Block
Once installed and activated, go to Custom Fields menu >> Add New Field.
It would be better to name it with a distinctive name so that you won’t be wondering what the heck it does in the future. In this case, we will name it a Custom Author Code Box.
Add The Call Field Code in Your Single.Php File
Go to Appearance >> Theme File Editor.
Depending on your theme, there can be a couple of single.php files with extensions in them such as single-****.php. Just find the one that has single.php on it. Add this code to wherever you want the code to appear:
<?php if(is_author($author='3')) : ?>
<?php echo the_field('custom_author_code_box'); ?>
<?php endif; ?>
CSS is not added here. So you can add some HTML markup if you want such as putting it inside a <div> and adding a CSS class in it to accommodate your target ads to see here.
Add the Custom Code Inside Each Post
And that’s it. You can add your custom code within the editor in your post edit page.
In case that you are unable to see the meta box for the Custom Author Code Box, toggle the Screen Options at the upper right corner and check it as it might be hidden by default. After toggling this, you will see the meta box to put your custom code into.
Making it Global Code For All Post By An Author
If for a reason you want the code to be the same for all post under an author. Make sure to make it a global field inside ACF.
Alternatively, you can edit the custom field and put in the code as a default value inside your custom_author_code_box field.
If you are having difficulty, feel free to contact me and I’ll see what I can do to help you implement these changes.
Thank you for reading and have a great day!
Leave a Reply