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

shortcode - how can i show WordPress custom field data to my short code?

programmeradmin1浏览0评论

I want to pull Sub Heading text using WordPress default custom field? i use this shortcode. but not showing any text?

//Shortcode: [lplist catname="" lpimgbg="" lpcatheading="" lpbtnurl=""]
function hm_post_list_shortcode($atts, $content) {
ob_start(); 
$plistshortcode = shortcode_atts( array(
    'catname'=>'$catatt',
    'lpimgbg' => 'http://localhost/example/wp-content/uploads/2019/04/rotating-Whetstone-bass-fishing.jpg',
        'lpcatheading'  => 'Heading Here',
        'lpbtnurl' => ''
), $atts);?>
<div class="lpcontainer" style="background-image: url(<?php echo esc_url($plistshortcode['lpimgbg']); ?>);">
    <div class="lpcontent-box">
        <h3><?php echo esc_html($plistshortcode['lpcatheading']); ?></h3>
        <div class="lplistitem">
            <?php
       $the_query = new WP_Query(array(
            'posts_per_page' => -1,
            'category_name' => $plistshortcode['catname']
        ));?>
            <?php while($the_query->have_posts()) : $the_query->the_post(); ?>
            //Wordpress Default custom field
            <?php $sub_heading = get_post_meta($post->ID, 'sub_heading', true);?>
            <a href="<?php echo esc_url(the_permalink()); ?>"><?php echo $sub_heading; ?></a>
            <?php endwhile;
            wp_reset_postdata();
        ?>
        </div>
        <div class="lpbtncontainer">
            <a href="<?php echo esc_url($plistshortcode['lpbtnurl']); ?>"><?php echo esc_html('View Full List');?></a>
        </div>
    </div>
</div>
<?php $hm_postlist = ob_get_clean();
return $hm_postlist;
}
add_shortcode( 'lplist', 'hm_post_list_shortcode' );

I want to pull Sub Heading text using WordPress default custom field? i use this shortcode. but not showing any text?

//Shortcode: [lplist catname="" lpimgbg="" lpcatheading="" lpbtnurl=""]
function hm_post_list_shortcode($atts, $content) {
ob_start(); 
$plistshortcode = shortcode_atts( array(
    'catname'=>'$catatt',
    'lpimgbg' => 'http://localhost/example/wp-content/uploads/2019/04/rotating-Whetstone-bass-fishing.jpg',
        'lpcatheading'  => 'Heading Here',
        'lpbtnurl' => 'https://www.google'
), $atts);?>
<div class="lpcontainer" style="background-image: url(<?php echo esc_url($plistshortcode['lpimgbg']); ?>);">
    <div class="lpcontent-box">
        <h3><?php echo esc_html($plistshortcode['lpcatheading']); ?></h3>
        <div class="lplistitem">
            <?php
       $the_query = new WP_Query(array(
            'posts_per_page' => -1,
            'category_name' => $plistshortcode['catname']
        ));?>
            <?php while($the_query->have_posts()) : $the_query->the_post(); ?>
            //Wordpress Default custom field
            <?php $sub_heading = get_post_meta($post->ID, 'sub_heading', true);?>
            <a href="<?php echo esc_url(the_permalink()); ?>"><?php echo $sub_heading; ?></a>
            <?php endwhile;
            wp_reset_postdata();
        ?>
        </div>
        <div class="lpbtncontainer">
            <a href="<?php echo esc_url($plistshortcode['lpbtnurl']); ?>"><?php echo esc_html('View Full List');?></a>
        </div>
    </div>
</div>
<?php $hm_postlist = ob_get_clean();
return $hm_postlist;
}
add_shortcode( 'lplist', 'hm_post_list_shortcode' );
Share Improve this question edited Jun 15, 2019 at 7:57 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jun 15, 2019 at 6:41 ArafatArafat 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You're using $post->ID, but haven't defined $post anywhere. You either need to specify global $post;, to get the current post in the loop, or define it some other way. But the best way to get this ID would just be to use get_the_ID():

<?php $sub_heading = get_post_meta( get_the_ID(), 'sub_heading', true ); ?>
发布评论

评论列表(0)

  1. 暂无评论