dongtu9823
2017-01-20 20:18
采纳率: 0%
浏览 41
已采纳

如何在WordPress页面上创建小部件时动态编号?

The WordPress website that I am creating has an Frequently Asked Questions page. Each question/answer pair is created via use of the SiteOrigin Page Builder Plugin and a custom widget. I would like to be able to add new questions and rearrange them without needing to manually renumber.

This is how it looks right now:

SiteOrigin Page Builder View

I would simply like the questions to be dynamically numbered (1., 2., 3., and soforth, just before the question).

I can think of some hack-ish ways that might accomplish this task, like abusing an <ol> tag or global variable. I might also be able to use some code to count the number of elements that have been created with the class "question", but it feels like that would add unnecessary code/latency to what should possible with a simple integer variable that exists only while this page is being rendered.

Is possible in WordPress? If so, how do I accomplish it? If not, is there a better method than what I have mentioned?

If it helps, here's the render code from my widget's PHP file:

public function widget( $args, $instance )
{
    $question = $instance['question'];
    $answer = $instance['answer'];

    echo $args['before_widget'];

    if ( ! empty( $question ) )
    {
        echo '<h3 class="question">';
        echo '. ';
        echo $question;
        echo '</h3>';
    }
    if ( ! empty( $answer ) )
    {
        echo '<p>';
        echo $answer;
        echo '</p>';
    }

    echo $args['after_widget'];
}

图片转代码服务由CSDN问答提供 功能建议

我正在创建的WordPress网站上有一个常见问题解答页面。 每个问题/答案对都是通过使用 SiteOrigin Page Builder插件和自定义小部件创建的。 我希望能够添加新问题并重新排列它们,而无需手动重新编号。

现在的样子如下:

< a href =“https://i.stack.imgur.com/mG4Ht.png”rel =“nofollow noreferrer”>

我想简单地将问题动态编号(1.,2.,3。和,在问题之前) 。

我可以想到一些可能完成此任务的黑客方法,例如滥用&lt; ol&gt; 标签或全局变量。 我也许可以使用一些代码来计算用“问题”类创建的元素的数量,但感觉这会增加不必要的代码/延迟,使用仅存在的简单整数变量应该可能 此页面正在呈现。

可以在WordPress中使用吗? 如果是这样,我该如何完成它? 如果没有,是否有比我提到的更好的方法?

如果有帮助,这里是我的小部件PHP文件中的渲染代码:

  公共功能小部件($ args,$ instance)
 {
 $ question = $ instance ['question']; 
 $ answer = $ instance ['answer']; 
 
 echo $ args [  'before_widget']; 
 
 if if(!empty($ question))
 {
 echo'&lt; h3 class =“question”&gt;'; 
 echo'。  '; 
 echo $ question; 
 echo'&lt; / h3&gt;'; 
} 
 if(!empty($ answer))
 {
 echo'&lt; p&gt;'; 
 echo $ 回答; 
 echo'&lt; / p&gt;'; 
} 
 
 echo $ args ['after_widget']; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxin8392 2017-01-20 21:52
    已采纳

    There's a number of ways to do this, but I ended up using Advanced Custom Fields (which I was already using for other things) to store/set a variable. After setting up an integer variable in ACF, I did the following (in my Widget PHP file):

    public function widget( $args, $instance )
    {
        $question = $instance['question'];
        $answer = $instance['answer'];
    
        echo $args['before_widget'];
    
        if ( ! empty( $question ) )
        {
            echo '<h3 class="question">';
            $question_count = (int) get_field('question_count');
            $question_count++;
            update_field('question_count', $question_count);
            echo $question_count;
            echo '. ';
            echo $question;
            echo '</h3>';
        }
        if ( ! empty( $answer ) )
        {
            echo '<p>';
            echo $answer;
            echo '</p>';
        }
    
        echo $args['after_widget'];
    }
    

    I'm going to leave the post up for a couple of days to see if anyone comes up with a better answer without the use of ACF, in case someone down the road has a different use-case than I do.

    点赞 打赏 评论

相关推荐 更多相似问题