最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - get_the_ID() doesnt work

programmeradmin4浏览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

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

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 badges
Add a comment  | 

2 Answers 2

Reset to default 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>

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论