admin管理员组

文章数量:1335386

I am trying to add some html code at the start and end of all posts but this HTML code need to change depending upon the category of the post.

The HTML code is a button to a quiz page. This quiz pages are created as per categories of wordpress.

So for example, all posts in the banking category need to link out to the quiz on banking. All posts in the marketing category need to link out to the quiz on marketing and so on.

I tried shortcoder plugin and WP quad ads to add my HTML code but stuck at how to different HTML code spending upon the category of post.

I am trying to add some html code at the start and end of all posts but this HTML code need to change depending upon the category of the post.

The HTML code is a button to a quiz page. This quiz pages are created as per categories of wordpress.

So for example, all posts in the banking category need to link out to the quiz on banking. All posts in the marketing category need to link out to the quiz on marketing and so on.

I tried shortcoder plugin and WP quad ads to add my HTML code but stuck at how to different HTML code spending upon the category of post.

Share Improve this question asked May 31, 2020 at 16:30 SandeepSandeep 1771 gold badge1 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can edit the template associated with your Category template in your theme. If you don't own the theme, you can make a Child theme of it to modify things.

For example, in my local WordPress dev environment, I have the theme TwentyTwenty active, and the template responsible for displaying is the index.php (typically it's the category.php or so). And the index.php is loading the /template-parts/content.php for displaying each of the post.

Now it's dead simple to add whatever you want to add and however. Suppose the code below is responsible for displaying the button you can see in the image:

<!-- </div> --> <!-- .section-inner -->

<?php if ( is_category() ) { ?>
    <div class="section-inner medium has-text-align-center">
        <button class="button" data-id=<?php the_ID(); ?>>Participate in Quiz</button>
    </div>
<?php } ?>

I passed a data-id attribute to each of the buttons, so that you can do whatever you want with each of the button individually.

本文标签: categoriesAdd custom code at start and end of all posts in a category