dqy0707 2015-08-11 14:02
浏览 91
已采纳

在小部件中调用Yoast SEO标题不起作用

I just install Wordpress SEO plugin by Yoast, test the title re-write function and it works in my title bar (single post). However when I try to call the title in widget, I just get error message

Warning: Missing argument 1 for kpndg()...

I insert this code in theme functions.php

add_filter( 'wpseo_title', 'kpndg' );
function kpndg( $title )  {
    return $title;
} 

Then call it in my widget:

$out.='<div class="trending-bar add-active bar-' . $i . '">';

    $out.='<a class="trending-link" href="'.get_permalink().'">&nbsp;</a>'; 

    $out.='<div class="title">'.kpndg().'</div>';

    $out.='<div class="trending-color-wrapper">';
        $out.='<div class="trending-color-layer"></div>';
        $out.='<div class="trending-color"></div>';
        $out.='<div class="trending-meta">' . $meta . '</div>';
    $out.='</div>';

$out.='</div>';

I can't figure out what is the problem in that code.

FYI: I use Engine theme by Industrial Themes

  • 写回答

1条回答 默认 最新

  • doufei8250 2015-08-11 14:04
    关注

    This line: $out.='<div class="title">'.kpndg().'</div>';

    You need to specify an argument... Or, alternatively, set a default value for the $title argument:

    add_filter( 'wpseo_title', 'kpndg' );
    function kpndg( $title = 'foo' )  {
        return $title;
    } 
    

    Edit

    Are you sure you didn't mean to use get_post_meta(get_the_ID(), '_yoast_wpseo_title', true); in place of kpndg()?

    Edit #2

    Instead of adding a new filter for wpseo_title, just create a "helper function":

    function kpndg() {
      return get_post_meta(get_the_ID(), '_yoast_wpseo_title', true) ?: get_the_title();
    }
    

    Then, within the loop, you can call echo kpndg()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度