【问题标题】:How to get post url in shortcode如何在短代码中获取帖子网址
【发布时间】:2018-08-08 17:52:50
【问题描述】:

我在函数中创建了简码,以便在编辑器中使用简码在页面/帖子上显示促销信息。

    //Testimonial shortcode
    add_shortcode('promotions', 'adenPromotions');   
    function adenPromotions($attr, $content)
   {        
   ob_start();  
    get_template_part('./templates/promotions-loop');  
    $ret = ob_get_contents();  
    ob_end_clean();  
    return $ret;    
   }

这是我拥有的模板文件中的代码。

<?php
if ($_SESSION['selectLocation'] != '') {
$args = array( 'post_type' => 'promotion', 
          'meta_key' => 'builder',
          'posts_per_page' => 3,
          'meta_value' => $_SESSION['selectLocation']);
} else {
$args = array( 'post_type' => 'promotion',
          'posts_per_page' => 3,
          'meta_value' => 0); 
};
if ($_SESSION['selectLocation'] != ''):
?>
<div class="container-fluid px-0">
<div class="row">
    <div class="col-md-12">
        <div class="offer-section-page-ah">
            <?php
                $the_query = new WP_Query( $args );?>              
                <?php if ( $the_query->have_posts() ) : $i = 0; ?>
                    <?php while ( $the_query->have_posts() ) : 
$the_query->the_post(); $i++ ?>
                        <div class="offer-loop-page  block-<?php echo $i 
?>">
                            <div class="offer-banner-ah" 
style="background-image:url('<?php echo the_post_thumbnail_url('full') ;?>');"></div>
                            <div class="offer-right">
                                <a href="<?php echo get_permalink() ;?>"><h2 class="heading"><?php the_title() ;?></h2></a>
                                <div class="post-expirator-ah"><p><?php echo do_shortcode('[postexpirator]') ;?></p></div>
                                <div class="sub-heading"><p><?php the_excerpt() ;?></p></div>
                                <div class="more-btn-ah"><a href="<?php echo get_permalink() ;?>" class="pink">Find out more</a></div>
                            </div>
                        </div>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>        
                <?php else : ?>
                    <h2 class="heading text-center mb-0 m-0 py-4"> Sorry, no promotion available in your region.</h2>
            <?php endif; ?>
        </div>
    </div>
</div>

get_permalink() 应该在循环中返回帖子的 url,但是如果有意义的话,它会返回添加了短代码的帖子的 URL。

希望代码有意义。如果这里的代码没有意义,这里是 pastebin 版本。 -> https://pastebin.com/MSNn6rKQ

编辑:显然 the_excerpt() 函数正在播放,不知道为什么,删除它并解决了问题。而是为描述创建了 ACF 字段。

【问题讨论】:

  • 你试过get_post_permalink()吗?

标签: php wordpress


【解决方案1】:

你应该试试这个:

get_permalink(get_the_ID()) 而不是 get_the_permalink

【讨论】:

  • 这也返回页面的url。想知道为什么 the_title() 和其他东西可以工作,但是 get_permalink();
  • 您是否尝试过使用 get_the_permalink 而不是 get_permalink?
【解决方案2】:

在 Post 循环中,您可以使用 get_the_ID() 函数..

通过这个函数,你可以在循环中获取帖子的ID..

并使用此 ID 在循环中获取帖子的永久链接

$post_id = get_the_ID();
get_the_permalink($post_id);

【讨论】:

  • get_the_ID() 返回添加短代码的页面的帖子 ID,而不是循环内的帖子。
【解决方案3】:

要获取有关使用您的简码的当前帖子的信息,请使用:

global $post;

你可以

get_the_permalink($post->ID)

【讨论】:

  • 我知道这很旧,但问题是关于在循环中检索永久链接,而不是使用短代码的帖子。
猜你喜欢
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多