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

如何在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'];
}
  • 写回答

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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!