0

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

1

get_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>
| improve this answer | |
0

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 . '"]' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.