Here is my query loop:
<?php
$query = new \WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
I want to call $post for this:
<div>
<?php echo do_shortcode( '[svg-flag flag="' . get_post_meta( $post->ID, 'ozellikler_text', true ) . '"]' ); ?>
</div>
I tried to call it with this:
<?php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
?>
the code doesn't call it. It gives the error:
Fatal error: Cannot redeclare get_the_ID() (previously declared in
Here is my query loop:
<?php
$query = new \WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
I want to call $post for this:
<div>
<?php echo do_shortcode( '[svg-flag flag="' . get_post_meta( $post->ID, 'ozellikler_text', true ) . '"]' ); ?>
</div>
I tried to call it with this:
<?php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
?>
the code doesn't call it. It gives the error:
Share Improve this question edited Jan 25, 2020 at 18:05 Max Yudin 6,3882 gold badges26 silver badges36 bronze badges asked Jan 25, 2020 at 17:38 Faruk rızaFaruk rıza 982 silver badges11 bronze badgesFatal error: Cannot redeclare get_the_ID() (previously declared in
2 Answers
Reset to default 1get_the_ID is a WordPress core fuction in the global namespace, so you can't make a second function called get_the_ID
as it won't know which one to use. You should just call get_the_ID() without writing a new function.
For your example code, you could do something like this:
<div>
<?php echo do_shortcode( '[svg-flag flag="' . get_post_meta( get_the_ID(), 'ozellikler_text', true ) . '"]' ); ?>
</div>
First, you don't have such thing as $post->ID
, you have $query->$post->ID
in your WP_Query result:
<?php
$my_post_meta = get_post_meta(
$query->$post->ID, // note this
'ozellikler_text',
true
);
echo do_shortcode( '[svg-flag flag="' . $my_post_meta . '"]' );
Second, as already written in the error text, you can't redeclare already declared built-in function. Simply use it:
<?php
$my_post_meta = get_post_meta(
get_the_ID(), // use the built-in function
'ozellikler_text',
true
);
echo do_shortcode( '[svg-flag flag="' . $my_post_meta . '"]' );