admin管理员组文章数量:1435859
I have used a form with an Ajax action to pull post information through on submit.
It works a charm. However, I have used categories to separate the work into categories. One of which is the brand.
Within the page template the script I use works, but when used in the functions.php file. It doesn't pull the required results through.
I think it may be something to do with when the query to the post is triggered or how the add_action is set up.
Can someone please help me pull through the name of the category found within the ID of 31? When used in the functions.php.
Here is what I have written:
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo "<div class=\"portfolio-piece\" style=\"background-image: url(" . get_the_post_thumbnail_url() . ");\">";
echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";
echo "<div class=\"portfolio-piece-hover\">";
echo "</div>";
echo "<div class=\"portfolio-piece-inner\">";
echo "<h5 class=\"portfolio-tags\">" . the_tags('', ' / ', '') . "</h5>";
echo "<h4 class=\"portfolio-title\">";
echo the_title();
echo "</h4>";
echo "<h4 class=\"portfolio-company\">";
$brands = wp_get_post_terms( $post->ID, 'category', array(
'orderby' => 'name',
'order' => 'DESC',
'parent' => 31
) );
foreach ( $brands as $brand ) {
echo $brand->name;
};
echo "</h4>";
echo "</div>";
echo "</div>";
endwhile;
wp_reset_postdata();
Where literally everything works but the portfolio-company / $brands = wp_get_post_terms part.
Thanks, Jason.
I have used a form with an Ajax action to pull post information through on submit.
It works a charm. However, I have used categories to separate the work into categories. One of which is the brand.
Within the page template the script I use works, but when used in the functions.php file. It doesn't pull the required results through.
I think it may be something to do with when the query to the post is triggered or how the add_action is set up.
Can someone please help me pull through the name of the category found within the ID of 31? When used in the functions.php.
Here is what I have written:
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo "<div class=\"portfolio-piece\" style=\"background-image: url(" . get_the_post_thumbnail_url() . ");\">";
echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";
echo "<div class=\"portfolio-piece-hover\">";
echo "</div>";
echo "<div class=\"portfolio-piece-inner\">";
echo "<h5 class=\"portfolio-tags\">" . the_tags('', ' / ', '') . "</h5>";
echo "<h4 class=\"portfolio-title\">";
echo the_title();
echo "</h4>";
echo "<h4 class=\"portfolio-company\">";
$brands = wp_get_post_terms( $post->ID, 'category', array(
'orderby' => 'name',
'order' => 'DESC',
'parent' => 31
) );
foreach ( $brands as $brand ) {
echo $brand->name;
};
echo "</h4>";
echo "</div>";
echo "</div>";
endwhile;
wp_reset_postdata();
Where literally everything works but the portfolio-company / $brands = wp_get_post_terms part.
Thanks, Jason.
Share Improve this question asked Mar 19, 2019 at 11:46 Jason Is My NameJason Is My Name 3782 gold badges7 silver badges21 bronze badges1 Answer
Reset to default 1(Revised answer)
So I'm assuming that there are actually posts in the category (ID: 31
), and based on the wp_get_post_terms()
reference:
$post_id
(int) (Optional) The Post ID. Does not default to the ID of the global$post
. Default 0.
It's likely that you're not passing the proper post ID to the wp_get_post_terms()
function:
wp_get_post_terms( $post->ID, 'category', array(...) );
In that code, you have $post
and yet, it's not defined anywhere in your code as in the question.
So if you're trying to get the ID of the current post in the WordPress Loop, where the post data is stored in the global $post
variable, then you should first make the variable accessible in your function/code using global $post;
like so:
global $post;
// here and after this line, you may now use $post->ID
if( $query->have_posts() ) :
...
endif;
Or you could also use get_the_ID()
without having to do global $post;
, particularly if all you wanted is to get the post ID:
wp_get_post_terms( get_the_ID(), 'category', array(...) ); // here I don't use $post->ID
And it's probably worth mentioning that in standard templates such as page.php
, the global $post
variable is already "global-ed", hence you may access the $post
anywhere in the template. (Well, not exactly "anywhere"; however, I'm not going to dig deeper into that in this answer..)
Additional Notes
This is probably just a typo, but in the code in the question, you got a syntax error here:
foreach ( $brands as $brand ) {
echo $brand->name;
}; // <- remove that ;
And you forgot the closing endif;
..
本文标签: wp queryGetting the post terms 39wpgetpostterms39 per post when within the functionsphp file
版权声明:本文标题:wp query - Getting the post terms 'wp_get_post_terms' per post when within the functions.php file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745675408a2669823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论