I am trying to insert a part of my template into a Category Archive page.
How would I go about that?
I guess I'd need something like this:
<?php
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;
if ($catid == 10) {
get_template_part('1','2') << ?
}
if ($catid == 7) {
get_template_part('3','4') << ?
}
}
?>
I am trying to insert a part of my template into a Category Archive page.
How would I go about that?
I guess I'd need something like this:
<?php
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;
if ($catid == 10) {
get_template_part('1','2') << ?
}
if ($catid == 7) {
get_template_part('3','4') << ?
}
}
?>
Share
Improve this question
asked Jan 28, 2022 at 2:12
HenryHenry
9831 gold badge8 silver badges31 bronze badges
4
|
1 Answer
Reset to default 0I think the best way is hooking to the filter template_include
https://developer.wordpress.org/reference/hooks/template_include/
You'll find some exmple at https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/
1-2.php
(then1.php
) and3-4.php
(then3.php
) in your theme. Also, to answer theelse
question: you'd need anelse
statement if you want to set a default template-part to load (ie if$catid
is neither7
nor10
). – Pat J Commented Jan 28, 2022 at 2:20<<?
text, which I assume were meant as pointers for us to look at and not actual parts of your code. – Pat J Commented Jan 28, 2022 at 3:19