duanqianhuan3994 2014-12-15 20:59
浏览 37
已采纳

如何使用wordpress小部件内的动态链接(或使用其他php函数)

I have a wordpress website, and I have 2 separate, but related questions:

In my footer, I have 3 widgets (forming 3 columns inside of the footer area).

~~~~~~

  1. Inside of one of these widgets, I wanted to include this kind of coding to dynamically populate the year and the name of the website:

    <?php echo date( 'Y' ); ?>. &copy; <?php bloginfo( 'name' ); ?>.<br />ALL Rights Reserved.<br />
    

I know I can't used PHP coding inside of the widget's text area.. so how can I get it to dynamically provide the current year, and the name of the Website - without hard coding it, and having to change the year each year? (I ended up having to pull this function outside of the widget, and placed it inside the actual footer.php page. That works.. but not the solution I was looking for.)

~~~~~~

  1. More importantly, in one of the other footer widgets, I have a 'sitemap' area, providing some links to various pages of the site.

If it wasn't inside a widget, I would just code the page links like this:

 <a href="<?php bloginfo( 'template_directory' ); ?>/page-name/">

Again, since I can't code php inside the widget, I tried just hardcoding the link to the RELATIVE path page name, but that caused me problems.

<a href="about-us/">About Us</a>

It's OK when you click on the links from the home page, but when you are clicking on the footer links from an interior page.. it can't find it, since the starting directory structure is different from the root directory.

If I am to code the link using the absolute path, this will also a problem. The site is currently on my localhost. So I have to code the link like this:

 http://localhost/site2/localWP2/about-us/

which of course will have to be changed when I go live.

Is there a standard way you can dynamically code a page's directory structure for its URL link from inside widgets? Especially those widgets that are located inside the footer, and will be accessed from every page.. each of those pages being inside of their own directory.. which of course causes problems with relative references!

Or is the only choice to hardcode the direct URLs for each link, then have to go back and change them all when you go live?

~~~~~~

Thanks for any advice!

  • 写回答

1条回答 默认 最新

  • douzhuang1900 2014-12-15 21:04
    关注

    I love this plugin for processing PHP code within a widget:

    https://wordpress.org/plugins/php-code-widget/

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路