douhuangjian9627 2019-08-13 14:03
浏览 41

如何使用/嵌入此Wordpress代码段。 (链接到类别中的最新帖子)

I have 3 menu points. Each of these is supposed to link to the newest post in its category. This can be done manually with the Wordpress Menu. But I want this Link to update automatically. It should link to the Postpage.

Since I have no php knowledge I found the snippet below and followed the instructions.

Got the Snippet from here: http://t-machine.org/index.php/2016/05/19/wordpress-plugin-insert-link-to-latest-post-in-category-on-your-menu/

/** Adam: add support for putting 'latest post in category X' to menu:  */
// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );
}

// Replaces a custom URL placeholder with the URL to the latest post
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {

    $key = 'http://#latestpost:';

   // Loop through the menu items looking for placeholder(s)
   foreach ( $items as $item ) {

    // Is this the placeholder we're looking for?
    if ( 0 === strpos( $item->url, $key ) )
    {

    $catname = substr( $item->url, strlen($key) );
    // Get the latest post
    $latestpost = get_posts( array(
        'posts_per_page' => 1,
            'category_name' => $catname
    ) );

    if ( empty( $latestpost ) )
        continue;

    // Replace the placeholder with the real URL
    $item->url = get_permalink( $latestpost[0]->ID );
    }
}

// Return the modified (or maybe unmodified) menu items array
return $items;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?