admin管理员组文章数量:1130349
The value is not fetching and displaying in this plugin. When I add the code elsewhere in the theme it's working properly.
<?php
function related_posts( $atts ) {
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 5,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<li style="list-style: none;"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
$gread = get_post_meta($post->ID, 'read_time', true);
$gdate = get_the_date();
echo '<span style="font-size:12px; margin-left: 22px;"> Read Time: '.$gread.' Minutes </span>';
echo '<span style="font-size:12px; margin-left: 22px;"> Posted: '.$gdate.' </span>';
endwhile;
// reset $post after custom loop end
}
add_shortcode( 'relatedpost', 'related_posts' );
The value is not fetching and displaying in this plugin. When I add the code elsewhere in the theme it's working properly.
<?php
function related_posts( $atts ) {
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 5,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<li style="list-style: none;"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
$gread = get_post_meta($post->ID, 'read_time', true);
$gdate = get_the_date();
echo '<span style="font-size:12px; margin-left: 22px;"> Read Time: '.$gread.' Minutes </span>';
echo '<span style="font-size:12px; margin-left: 22px;"> Posted: '.$gdate.' </span>';
endwhile;
// reset $post after custom loop end
}
add_shortcode( 'relatedpost', 'related_posts' );
Share
Improve this question
edited Dec 14, 2018 at 11:44
Max Yudin
6,3982 gold badges26 silver badges36 bronze badges
asked Dec 14, 2018 at 11:12
Praveen KalarikkalPraveen Kalarikkal
1
6
|
Show 1 more comment
1 Answer
Reset to default 0You don't have the $post object, but trying to use it in the code ($post->ID). Use get_the_ID() function instead:
<?php
while ( $cat_query->have_posts() ) : $cat_query->the_post();
// $gread = get_post_meta( $post->ID, 'read_time', true ); // Wrong
$gread = get_post_meta( get_the_ID(), 'read_time', true ); // Right
endwhile;
}
From now forth always enable debugging when you develop. In such a way you'll avoid wasting your time and asking unpractical questions.
The value is not fetching and displaying in this plugin. When I add the code elsewhere in the theme it's working properly.
<?php
function related_posts( $atts ) {
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 5,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<li style="list-style: none;"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
$gread = get_post_meta($post->ID, 'read_time', true);
$gdate = get_the_date();
echo '<span style="font-size:12px; margin-left: 22px;"> Read Time: '.$gread.' Minutes </span>';
echo '<span style="font-size:12px; margin-left: 22px;"> Posted: '.$gdate.' </span>';
endwhile;
// reset $post after custom loop end
}
add_shortcode( 'relatedpost', 'related_posts' );
The value is not fetching and displaying in this plugin. When I add the code elsewhere in the theme it's working properly.
<?php
function related_posts( $atts ) {
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 5,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<li style="list-style: none;"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
$gread = get_post_meta($post->ID, 'read_time', true);
$gdate = get_the_date();
echo '<span style="font-size:12px; margin-left: 22px;"> Read Time: '.$gread.' Minutes </span>';
echo '<span style="font-size:12px; margin-left: 22px;"> Posted: '.$gdate.' </span>';
endwhile;
// reset $post after custom loop end
}
add_shortcode( 'relatedpost', 'related_posts' );
Share
Improve this question
edited Dec 14, 2018 at 11:44
Max Yudin
6,3982 gold badges26 silver badges36 bronze badges
asked Dec 14, 2018 at 11:12
Praveen KalarikkalPraveen Kalarikkal
1
6
- "Not working" is a bad explanation of the problem. Be more specific. – Max Yudin Commented Dec 14, 2018 at 11:24
- Hi Max, sorry for my language, Not working in the sense the value is not fetching and displaying in this plugin. When i add the code elsewhere in the theme its working properly. – Praveen Kalarikkal Commented Dec 14, 2018 at 11:32
-
When you use the plugin what does
get_the_ID()returns inside the function? – Max Yudin Commented Dec 14, 2018 at 12:10 - ok. i need to add the short code to widgets basically. it works well except the value from the custom field not displaying/fetching to the loop. but get_the_date() is working properly. my coding experience is limited. if u can help me to resolve this issue, that is appreciable. – Praveen Kalarikkal Commented Dec 14, 2018 at 13:29
- hi, got the solution. instead of calling default method tried this one -> $custom_fields = get_post_custom( get_the_ID() ); $my_custom_field = $custom_fields['time_to_read']; foreach ( $my_custom_field as $key => $value ) – Praveen Kalarikkal Commented Dec 14, 2018 at 13:52
1 Answer
Reset to default 0You don't have the $post object, but trying to use it in the code ($post->ID). Use get_the_ID() function instead:
<?php
while ( $cat_query->have_posts() ) : $cat_query->the_post();
// $gread = get_post_meta( $post->ID, 'read_time', true ); // Wrong
$gread = get_post_meta( get_the_ID(), 'read_time', true ); // Right
endwhile;
}
From now forth always enable debugging when you develop. In such a way you'll avoid wasting your time and asking unpractical questions.
本文标签: Trying to get custom field data in a simple plugin
版权声明:本文标题:Trying to get custom field data in a simple plugin 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749096770a2315378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_the_ID()returns inside the function? – Max Yudin Commented Dec 14, 2018 at 12:10