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 badges1 Answer
Reset to default 1You 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
版权声明:本文标题:categories - Add custom code at start and end of all posts in a category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742385532a2464956.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论