how to display wordpress blog post categories in external html/php website
Get the solution ↓↓↓I have been trying to display wordpress blog post categories in a html/php website. This is my code.
<select class="form-control" name="Category">
<option>Select Option</option>
<?php
include('blog/wp-load.php');
$category_list = wp_get_post_categories();
foreach($category_list as $category){ ?>
<option><?php echo $category->category_name; ?></option>
<?php } ?>
</select>
Its not working. What is wrong with my code? Is this the right method? Can anyone please help me with the solution?
Answer
Solution:
wp_get_post_categories() require post_id to render data of post if you don't pass post id wordpress get post_id from global $post obejct. In your case you are including wordpress outside of it's scope so this funciton never work.
Use get_terms() to get term of your taxonomy.
include('blog/wp-load.php');
$category_list = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
?>
if( !empty( $category_list ) && !is_wp_error( $category_list ) ){
?>
<select class="form-control" name="Category">
<option>Select Option</option>
<?php foreach( $category_list as $category ){ ?>
<option value="<?php echo $category->term_id; ?>"><?php echo $category->name; ?></option>
<?php } ?>
</select>
<?php } ?>
Although I not recommend this method instead of this you can call WordPress API endpoint to get your taxonomy. You can refer here
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: filter_sanitize_string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.