admin管理员组文章数量:1310178
I'm trying to exclude 2 (and potentially more) ID's from my loop, that displays all categories that have posts. The category ID's i'm currently trying to exclude are 1 and 19. I'm very new to PHP but have previously been able to fix my issues by searching on the forum but nothing so far has worked.
// Function to create shortcode of all categories
function my_categories_shortcode() {
// extracting categories
$args=array(
'orderby' => 'name',
'order' => 'ASC',
)
?><div class="categorylist">
<?php foreach (get_categories( $args ) as $cat ) : ?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<h3 class="categoryname"><?php echo $cat->cat_name; ?></h3></a>
<?php endforeach; ?>
</div>
<?php
}
// register shortcode
add_shortcode('showcategories', 'my_categories_shortcode');
What should i add, to exclude categories?
Thanks in advance!
EDIT
Thanks, i solved this by adding a continue function as mentioned by Tom Nowell. I have also added the hint that this is a string to the function:
// Function to create shortcode of all categories
function my_categories_shortcode() : string {
// extracting categories
$args=array(
'orderby' => 'name',
'order' => 'ASC',
)
?><div class="categorylist">
<?php foreach (get_categories( $args ) as $cat ) :
if ($cat->term_id == 19) {
continue;
}
?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<h3 class="categoryname"><?php echo $cat->cat_name; ?></h3></a>
<?php endforeach; ?>
</div>
<?php
}
// register shortcode
add_shortcode('showcategories', 'my_categories_shortcode');
I'm trying to exclude 2 (and potentially more) ID's from my loop, that displays all categories that have posts. The category ID's i'm currently trying to exclude are 1 and 19. I'm very new to PHP but have previously been able to fix my issues by searching on the forum but nothing so far has worked.
// Function to create shortcode of all categories
function my_categories_shortcode() {
// extracting categories
$args=array(
'orderby' => 'name',
'order' => 'ASC',
)
?><div class="categorylist">
<?php foreach (get_categories( $args ) as $cat ) : ?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<h3 class="categoryname"><?php echo $cat->cat_name; ?></h3></a>
<?php endforeach; ?>
</div>
<?php
}
// register shortcode
add_shortcode('showcategories', 'my_categories_shortcode');
What should i add, to exclude categories?
Thanks in advance!
EDIT
Thanks, i solved this by adding a continue function as mentioned by Tom Nowell. I have also added the hint that this is a string to the function:
// Function to create shortcode of all categories
function my_categories_shortcode() : string {
// extracting categories
$args=array(
'orderby' => 'name',
'order' => 'ASC',
)
?><div class="categorylist">
<?php foreach (get_categories( $args ) as $cat ) :
if ($cat->term_id == 19) {
continue;
}
?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<h3 class="categoryname"><?php echo $cat->cat_name; ?></h3></a>
<?php endforeach; ?>
</div>
<?php
}
// register shortcode
add_shortcode('showcategories', 'my_categories_shortcode');
Share
Improve this question
edited Jan 14, 2021 at 15:28
Tom J Nowell♦
61k7 gold badges79 silver badges148 bronze badges
asked Jan 14, 2021 at 12:47
Johan Schack PetersenJohan Schack Petersen
112 bronze badges
5
|
1 Answer
Reset to default 0The solution is already in your code:
<?php foreach (get_categories( $args ) as $cat ) : ?>
<a class="categorylink" href="<?php echo get_category_link($cat->term_id); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
More specifically:
<?php foreach (get_categories( $args ) as $cat ) : ?>
...get_category_link($cat->term_id)...
That term_id
is the ID of the category, so just test if it matches one of your undesired IDs and continue;
to skip to the next one.
However, there is an unrelated but super critical mistake in your code. Shortcodes do not echo/output directly. They need to return their output in a string. This shortcode will break a lot of things, and will appear in the wrong place.
Remember:
function brokenshortcode() {
?><p>I am an awful shortcode that breaks lots of things</p><?php
}
add_shortcode( 'broken', 'brokenshortcode' );
function niceshortcode() : string {
return '<p>I am a good well behaved shortcode</p>';
}
add_shortcode( 'nice', 'niceshortcode' );
You can enforce this by type hinting the function like this:
function my_categories_shortcode() : string {
Now the function will cause a PHP fatal if you make this mistake
本文标签: phpHow to exclude category ID from Looper in Wordpress
版权声明:本文标题:php - How to exclude category ID from Looper in Wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741835332a2400181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
: string
must return a string, but as I mentioned in my answer, your shortcode is built incorrectly and does not return its content as a string. Shortcodes can't echo or output directly, but that's what your shortcode function does, which is wrong and incorrect. Shortcodes that output directly via?>stuff<?php
orecho 'stuff';
break REST APIs, XMLRPC, RSS feeds, they don't work inside nested shortcodes, and they appear at the start of a post, not the location you put the shortcode. Do not directly echo or output HTML in a shortcode! Put it in a variable and return it – Tom J Nowell ♦ Commented Jan 14, 2021 at 15:28