【问题标题】:How to format $output the right way?如何以正确的方式格式化 $output?
【发布时间】:2017-09-12 08:53:36
【问题描述】:

我不知道 php,我正在谷歌搜索代码,我想出了一些我需要在 Wordpress 网站上做的事情。 这段代码什么也没显示,但也没有破坏网站,我认为它不正确,所以有人可以帮助我。 这是代码:

$output.=       '<div class="column1">'

      $categories = get_the_category();
      $separator = ' ';
      $output = '';
      if($categories) {
         $output .= '<div class="above-entry-meta"><span class="cat-links">';
         foreach($categories as $category) {
               $output .= '<a href="'.get_category_link( $category->term_id ).'"  rel="category tag">'.$category->cat_name.'</a>'.$separator;
            }
      }
         $output .='</span></div>';

$output.=       '</div> <!-- end column1 -->';

我想通过简码输出当前的帖子类别,这是其中的一部分。 我只需要从这个例子中得到帮助,我将能够通过下面的例子继续编码。

【问题讨论】:

  • 你把这段代码放在哪里了?
  • 这一行$output = ''; 重置为空$output 的值,您应该将它移到此代码的最开始,以便不会从输出中删除开始的 div(编辑:或删除如果输出在此显示的代码之前已经有一个值,则完全是这样)
  • function getmycode($atts, $content = "") { 代码返回 $output; } add_shortcode('getmycode', 'getmycode');
  • 卡达斯,就是这样。谢谢。回答吧,我会接受的。非常感谢。对于像我这样的菜鸟来说,简单的事情可能很难。
  • 我认为这篇文章有一个实际的答案并没有真正的用处,如果问题解决了,这对我来说就足够了;)

标签: php wordpress output shortcode


【解决方案1】:

试试这个代码

$output = '';
$output .=  '<div class="column1">';
$args = array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 5 );
$categories = get_the_category($args);
$separator = ' ';
if($categories) {
$output .= '<div class="above-entry-meta"><span class="cat-links">';
 foreach($categories as $category) {
   $output .= '<a href="'.get_category_link( $category->term_id ).'"  rel="category tag">'.$category->cat_name.'</a>'.$separator;
 }
}
$output .='</span></div>';

$output.=       '</div> <!-- end column1 -->';

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2011-02-07
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多